linux 数组 下标访问,Objective-C如何自己实现一个基于数组下标的属性访问模式

本文介绍如何在Objective-C中利用非正式协议实现数组和字典的下标访问功能,并提供了一个自定义类MyContainer的示例,该类支持类似于NSArray、NSMutableArray、NSDictionary和NSMutableDictionary的下标访问方式。

在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

@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;

}

0b1331709591d260c1c78e86d0c51c18.png

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值