Objective-C如何自己實現一個基於陣列下標的屬性訪問模式

小溪彼岸發表於2016-06-22

原文地址:http://www.cnblogs.com/zenny-chen/p/3593660.html

在iOS6.0以及OS X10.8之後,Apple引入了一套非正式協議(informal protocol)與Objective-C語法直接繫結。當你實現了這其中的方法之後即可使用陣列下標來訪問屬性元素。

在Foundation庫中,NSArray類實現了- (id)objectAtIndexedSubscript:(NSUInteger)idx方法。因此,我們可以這麼來訪問陣列元素:

NSArray *arr = @[@100, @200, @300];
NSNumber *num = arr[0];

上述arr[0]就相當於[arr objectAtIndex:0]。

而NSMutableArray在基於NSArray的基礎上又實現了- (void)setObject:(id)anObject atIndexedSubscript:(NSUInteger)index方法。這樣我們可以通過陣列下標來讀寫相應元素,比如:

NSMutableArray *arr = [NSMutableArray arrayWithArray:@[@100, @200, @300]];
arr[2] = arr[0];

而NSDictionary類實現了- (id)objectForKeyedSubscript:(id)key方法。這樣我們能以陣列下標的形式來訪問相應鍵的值。比如:

NSDictionary *dict = @{@"key" : @"value"};
NSString *value = dict[@"key"];

而NSMutableDictionary在NSDictionary類的基礎上又實現了- (void)setObject:(id)object forKeyedSubscript:(id < NSCopying >)aKey方法。這樣,我們能以陣列下標的方式來讀寫相應鍵的值。比如:

NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithDictionary:@{@"key":"@Hello"}];
dict[dict[@"key"]] = @"world";

下面我們通過實現這四個方法,自己實現一個能同時使用這四種下標方式訪問模式的類。

//
//  main.m
//  objCTest
//
//  Created by Zenny Chen on 12-2-7.
//  Copyright (c) 2014年 Neon Media Studio. All rights reserved.
//

#import <Foundation/Foundation.h>


@interface MyContainer : NSObject
{
@private

    NSMutableDictionary *mDict;
    NSMutableArray *mArray;
}

- (void)setObject:(id)object forKeyedSubscript:(id < NSCopying >)aKey;
- (id)objectForKeyedSubscript:(id)key;
- (void)setObject:(id)anObject atIndexedSubscript:(NSUInteger)index;
- (id)objectAtIndexedSubscript:(NSUInteger)idx;

@end

@implementation MyContainer

- (instancetype)init
{
    self = [super init];

    mDict = [[NSMutableDictionary alloc] initWithDictionary:@{@"key1":@"value1", @"key2":@"value2"}];

    mArray = [[NSMutableArray alloc] initWithArray:@[@100, @200, @300, @400]];

    return self;
}

- (void)dealloc
{
    if(mDict != nil)
    {
        [mDict removeAllObjects];
        [mDict release];
        mDict = nil;
    }

    if(mArray != nil)
    {
        [mArray removeAllObjects];
        [mArray release];
        mArray = nil;
    }

    [super dealloc];
}

- (void)setObject:(id)object forKeyedSubscript:(id < NSCopying >)aKey
{
    [mDict setObject:object forKey:aKey];
}

- (id)objectForKeyedSubscript:(id)key
{
    return [mDict objectForKey:key];
}

- (void)setObject:(id)anObject atIndexedSubscript:(NSUInteger)index
{
    const NSUInteger length = [mArray count];
    if(index > length)
        return;

    if(index == length)
        [mArray addObject:anObject];
    else
        [mArray replaceObjectAtIndex:index withObject:anObject];
}

- (id)objectAtIndexedSubscript:(NSUInteger)idx
{
    if(idx >= [mArray count])
        return nil;

    return [mArray objectAtIndex:idx];
}

@end


int main (int argc, const char * argv[])
{
    @autoreleasepool
    {
        // insert code here...

        MyContainer *cont = [[MyContainer alloc] init];

        cont[@"mykey"] = @"myvalye";

        NSLog(@"key1 is: %@", cont[@"key1"]);
        NSLog(@"key2 is: %@", cont[@"key2"]);
        NSLog(@"mykey is: %@", cont[@"mykey"]);

        cont[4] = @500;
        cont[2] = @-300;

        NSLog(@"The value[4] = %@", cont[4]);
        NSLog(@"The value[3] = %@", cont[3]);
        NSLog(@"The value[2] = %@", cont[2]);
    }

    return 0;
}

相關文章