亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

NSArray,NSSet,NSDictionary總結(jié)

系統(tǒng) 1708 0

三種集合類來收集cocoa對象(NSObject對象):
NSArray ? 用于對象有序集合(相當于是數(shù)組)
NSSet ? 用于對象無序集合
NSDictionary 用于鍵值映射
以上三種集合類是不可變的(一旦初始化后,就不能改變)

以下是對應的三種可變集合類(這三種可變集合類是對應上面三種集合類的子類):
NSMutableArray
NSMutableSet
NSMutableDictionary

注:這些集合類只能收集cocoa對象(NSOjbect對象),如果想保存一些原始的C數(shù)據(jù)(例如,int, float, double, BOOL等),則需要將這些原始的C數(shù)據(jù)封裝成 NSNumber 類型的,NSNumber對象是cocoa對象,可以被保存在集合類中。

NSArray
Ordered collection of objects.?Immutable. You cannot add or remove objects to it once it’s created.
Important methods:
+ (id)array WithObjects :(id)firstObject, ...; ????// ?nil ?terminated?
- (int)count;
- (id)objectAtIndex:(int)index; ? ? ? ? ? ? ? ? ? ? // ? NSString *s1=[[myarray objectAtIndex: 0];
- (void)makeObjectsPerformSelector:(SEL)aSelector;
- (NSArray *)sortedArrayUsingSelector:(SEL)aSelector;
- (id)lastObject; // returns nil if there are no objects in the array (convenient)
注:
類方法array WithObjects ?可以創(chuàng)建an? autoreleased ?NSArray of the items.例如
@implementation MyObject
- (NSArray *)coolCats?{
return [NSArray arrayWithObjects:@“Steve”, @“Ankush”, @“Sean”, nil];
}
@end
Other convenient create with methods (all return autoreleased objects):
[NSString stringWithFormat:@“Meaning of %@ is %d”, @“l(fā)ife”, 42];
[NSDictionary dictionaryWithObjectsAndKeys:ankush, @“TA”, janestudent, @“Student”, nil];
[NSArray arrayWithContentsOfFile:(NSString *)path];
----- 創(chuàng)建數(shù)組 ?-----
??? //NSArray *array = [[NSArray alloc] initWithObjects:
? ? @"One",@"Two",@"Three",@"Four",nil];

? ? self.dataArray = array;
? ? [array release];

? ? //- (unsigned) Count;數(shù)組所包含對象個數(shù);
? ? NSLog(@"self.dataArray cound:%d",[self.dataArray count]);

? ? //- (id) objectAtIndex: (unsigned int) index;獲取指定索引處的對象;
? ? NSLog(@"self.dataArray cound 2:%@",[self.dataArray objectAtIndex:2]);

------? 從一個數(shù)組拷貝數(shù)據(jù)到另一數(shù)組(可變數(shù)級)? -------???

? ? //arrayWithArray:
? ? //NSArray *array1 = [[NSArray alloc] init];
? ? NSMutableArray *MutableArray = [[NSMutableArray alloc] init];
? ? NSArray *array = [NSArray arrayWithObjects:
? ?? ?? ?? ?? ?? ?? ? @"a",@"b",@"c",nil];
? ? NSLog(@"array:%@",array);
? ? MutableArray = [NSMutableArray arrayWithArray:array];
? ? NSLog(@"MutableArray:%@",MutableArray);

? ? array1 = [NSArray arrayWithArray:array];
? ? NSLog(@"array1:%@",array1);

??? //Copy

? ? //id obj;
? ? NSMutableArray *newArray = [[NSMutableArray alloc] init];
? ? NSArray *oldArray = [NSArray arrayWithObjects:
? ?? ?? ?? ?? ?? ?? ?? ? @"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h",nil];

? ? NSLog(@"oldArray:%@",oldArray);
? ? for(int i = 0; i < [oldArray count]; i++)
? ? {? ?? ???
? ?? ???obj = [[oldArray objectAtIndex:i] copy];
? ?? ???[newArray addObject: obj];
? ? }
? ? //? ???
? ? NSLog(@"newArray:%@", newArray);
? ? [newArray release];

//快速枚舉
????
//NSMutableArray *newArray = [[NSMutableArray alloc] init];
? ? NSArray *oldArray = [NSArray arrayWithObjects:
? ?? ?? ?? ?? ?? ?? ?? ? @"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h",nil];? ?
? ? NSLog(@"oldArray:%@",oldArray);

? ? for(id obj in oldArray)
? ? {
? ?? ???[newArray addObject: obj];
? ? }
? ? //? ???
? ? NSLog(@"newArray:%@", newArray);
? ? [newArray release];? ?

??? //Deep copy

? ? //NSMutableArray *newArray = [[NSMutableArray alloc] init];
? ? NSArray *oldArray = [NSArray arrayWithObjects:
? ?? ?? ?? ?? ?? ?? ?? ? @"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h",nil];? ?
? ? NSLog(@"oldArray:%@",oldArray);? ?
? ? newArray = (NSMutableArray*)CFPropertyListCreateDeepCopy(kCFAllocatorDefault, (CFPropertyListRef)oldArray, kCFPropertyListMutableContainers);
? ? NSLog(@"newArray:%@", newArray);
? ? [newArray release];? ?

??? //Copy and sort

? ? //NSMutableArray *newArray = [[NSMutableArray alloc] init];
? ? NSArray *oldArray = [NSArray arrayWithObjects:

NSMutableArray
Mutable version of NSArray.
- (void)addObject:(id)anObject;
- (void)insertObject:(id)anObject atIndex:(int)index;
- (void)removeObjectAtIndex:(int)index;
- (void)removeAllObjects;

----- 給數(shù)組分配容量 -----
??? //NSArray *array;
? ? array = [NSMutableArray? arrayWithCapacity :20];

----- 在數(shù)組末尾添加對象 -----
??? //- (void) addObject: (id) anObject;
? ? //NSMutableArray *array = [NSMutableArray? arrayWithObjects :@"One",@"Two",@"Three",nil];
? ? [array? addObject :@"Four"];
? ? NSLog(@"array:%@",array);

----- 刪除數(shù)組中指定索引處對象 -----???
??? //-(void) removeObjectAtIndex: (unsigned) index;? ?
? ? //NSMutableArray *array = [NSMutableArray arrayWithObjects:@"One",@"Two",@"Three",nil];
? ? [array? removeObjectAtIndex :1];
? ? NSLog(@"array:%@",array);

? -----? 數(shù)組枚舉 -----??
//1、- (NSEnumerator *)objectEnumerator;? //從前向后
? ? //NSMutableArray *array = [NSMutableArray arrayWithObjects:@"One",@"Two",@"Three",nil];
? ? NSEnumerator *enumerator;
? ? enumerator = [array objectEnumerator];

? ? id thingie;
? ? while (thingie = [enumerator nextObject]) {
? ?? ???NSLog(@"thingie:%@",thingie);
? ? }
//2、- (NSEnumerator *)reverseObjectEnumerator;? //從后向前
? ? //NSMutableArray *array = [NSMutableArray arrayWithObjects:
? ? @"One",@"Two",@"Three",nil];
? ? NSEnumerator *enumerator;
? ? enumerator = [array reverseObjectEnumerator];

? ? id object;
? ? while (object = [enumerator nextObject]) {
? ?? ???NSLog(@"object:%@",object);
? ? }
//3、快速枚舉
? ? //NSMutableArray *array = [NSMutableArray arrayWithObjects:
? ? @"One",@"Two",@"Three",nil];
? ? for(NSString *string in array)
? ? {
? ?? ???NSLog(@"string:%@",string);
? ? }

-----? NSValue(對任何對象進行包裝) -----
//將NSRect放入NSArray中
? ? NSMutableArray *array = [[NSMutableArray alloc] init];
? ? NSValue *value;
? ? CGRect rect = CGRectMake(0, 0, 320, 480);? ?
? ? value = [NSValue valueWithBytes:&rect objCType:@encode(CGRect)];
? ? [array addObject:value];
? ? NSLog(@"array:%@",array);
//從Array中 提取
? ? value = [array objectAtIndex:0];
? ? [value getValue:&rect];
? ? NSLog(@"value:%@",value);

----★ 使用NSMutableArray要防止內(nèi)存泄露 ★------
NSObject* p1 = [[NSObject alloc] init];
NSObject* p2 = [[NSObject alloc] init];
NSMutableArray* objectsArray = [[NSMutableArray alloc] init];

[objectsArray addObject:p1];
NSLog(@"p1 count:%d", [p1 retainCount]);// 輸出 2,也就是執(zhí)行追加對象后,對象的計數(shù)器也被加1
//[p1 release];
//NSLog(@"p1 count:%d", [p1 retainCount]);

//同樣做數(shù)組替換時
[objectsArray replaceObjectAtIndex:0 withObject:p2];
NSLog(@"p2 count:%d", [p2 retainCount]);// 輸出 2,同樣也是2
NSLog(@"p1 count:%d", [p1 retainCount]);// 輸出 1,對象p1仍然存在
//[p2 release];
//NSLog(@"p2 count:%d", [p2 retainCount]);

//執(zhí)行清空數(shù)組
[objectsArray removeAllObjects];
NSLog(@"p2 count:%d", [p2 retainCount]);// 輸出 1,對象p2仍然存在
//[p2 release];

由此可知,每次執(zhí)行上面的數(shù)組操作后,要執(zhí)行對象release,如上面注釋中的語句,才能保證內(nèi)存不泄露。


NSSet
Unordered collection of objects.
Immutable. You cannot add or remove objects to it once it’s created.
Important methods:
+ set WithObjects :(id)firstObj, ...; ?// ?nil ?terminated

- (int)count;
- (BOOL)containsObject:(id)anObject;
- (id)anyObject;
- (void)makeObjectsPerformSelector:(SEL)aSelector;
- (id)member:(id)anObject; // uses isEqual: and returns a matching object (if any)

NSMutableSet
Mutable version of NSSet.
+ (NSMutableSet *)set;
- (void)addObject:(id)anObject;
- (void)removeObject:(id)anObject;
- (void)removeAllObjects;
- (void)unionSet:(NSSet *)otherSet;
- (void)minusSet:(NSSet *)otherSet;
- (void)intersectSet:(NSSet *)otherSet;

NSDictionary
key-value, key-value, ..... 一系列鍵值對。
key(鍵)在整個dictionary是唯一的,通過key可以查詢其對應的一個或多個value(值)。
Hash table. Look up objects using a key to get a value.
Immutable. You cannot add or remove objects to it once it’s created.
Keys are objects which must implement. ?Keys are usually NSString objects.
- (NSUInteger)hash & - (BOOL)isEqual:(NSObject *)obj
Important methods:
+ dictionaryWithObjectsAndKeys: (id)firstObject, ...;
- (int)count;
- (id)objectForKey:(id)key;
- (NSArray *)allKeys;
- (NSArray *)allValues;
?----- 創(chuàng)建字典 ?-----
??? //- (id) initWithObjectsAndKeys;

? ? //NSDictionary *dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:@"One",@"1",@"Two",@"2",@"Three",@"3",nil];
? ? NSString *string = [dictionary objectForKey:@"One"];
? ? NSLog(@"string:%@",string);
? ? NSLog(@"dictionary:%@",dictionary);
? ? [dictionary release];

NSMutableDictionary
Mutable version of NSDictionary.
+ (NSMutableDictionary *)dictionary;
- (void)setObject:(id)anObject forKey:(id)key;
- (void)removeObjectForKey:(id)key;
- (void)removeAllObjects;
- (void)addEntriesFromDictionary:(NSDictionary *)otherDictionary;
?----- 創(chuàng)建可變字典 ?-----???
//創(chuàng)建
? ? NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
?
//添加字典
? ? [dictionary setObject:@"One" forKey:@"1"];
? ? [dictionary setObject:@"Two" forKey:@"2"];
? ? [dictionary setObject:@"Three" forKey:@"3"];
? ? [dictionary setObject:@"Four" forKey:@"4"];
? ? NSLog(@"dictionary:%@",dictionary);
//刪除指定的字典
? ? [dictionary removeObjectForKey:@"3"];
? ? NSLog(@"dictionary:%@",dictionary);

NSArray,NSSet,NSDictionary總結(jié)


更多文章、技術(shù)交流、商務合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 免费国产黄线在线观看视频 | 精品国产hd | 久久99久久99精品免观看不卡 | 一级毛片a女人刺激视频免费 | 成人短视频在线观看免费 | 日本一区二区三区四区在线观看 | 久久国产精品亚洲va麻豆 | 国产精品久久久精品视频 | 色综合一本 | 久久久免费 | 成人凹凸短视频在线观看 | 97视频免费公开成人福利 | 久久一精品| 性欧美另类老妇高清 | 99久热在线精品视频观看 | 婷婷第四色 | 国产精品久久久久一区二区三区 | 国产精品麻豆99久久 | 久久香蕉国产线看观看精品yw | 亚洲国产精久久久久久久 | 日本不卡高清免费v日本 | 国产高清视频免费 | 国产精品国产三级国产 | 特级无码a级毛片特黄 | 狠狠色噜噜狠狠狠狠狠色综合久久 | 毛片色毛片18毛片美女 | 成人短视频在线免费观看 | 亚洲高清在线观看 | 国产精品视频久久 | 黄色免费看视频 | 成人a网站 | ww亚洲ww亚在线观看 | 男人天堂999| 亚洲精品一区二区久久这里 | 天天翘夜夜洗澡天天做 | 91www在线观看 | 国产h视频免费观看 | 中文字幕在线精品视频万部 | 中日韩一区二区三区 | 97av麻豆蜜桃一区二区 | 毛片免费在线观看 |