歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Objective-C如何自己實現一個基於數組下標的屬性訪問模式

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

日期:2017/3/1 9:20:11   编辑:Linux編程

在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;
}

Objective-C中@property的所有屬性詳解 http://www.linuxidc.com/Linux/2014-03/97744.htm

Objective-C 和 Core Foundation 對象相互轉換的內存管理總結 http://www.linuxidc.com/Linux/2014-03/97626.htm

使用 Objective-C 一年後我對它的看法 http://www.linuxidc.com/Linux/2013-12/94309.htm

10個Objective-C基礎面試題,iOS面試必備 http://www.linuxidc.com/Linux/2013-07/87393.htm

Objective-C適用C數學函數 <math.h> http://www.linuxidc.com/Linux/2013-06/86215.htm

好學的 Objective-C 高清PDF http://www.linuxidc.com/Linux/2014-09/106226.htm

Copyright © Linux教程網 All Rights Reserved