标签:
int main(int argc, const char * argv[]) { @autoreleasepool { /*不可变数组*/ //创建不可变数组 方式一 NSArray *arr=[NSArray arrayWithObjects:@"周一",@"周二",@"周三",@"周四",@"周五",@"周六",@"周日",nil]; for(int i=0;i<[arr count];i++){ NSLog(@"%@",[arr objectAtIndex:i]); } //创建不可变数组 方式二 NSArray *arr1=@[@"周一",@"周二",@"周三",@"周四",@"周五",@"周六",@"周日"]; for(int i=0;i<[arr1 count];i++){ NSLog(@"%@",arr1[i]); } } }
int main(int argc, const char * argv[]) { @autoreleasepool { /*可变数组*/ NSMutableArray *marr=[NSMutableArray array]; for (int i=0; i<3; i++) { marr[i]=@(i+1); } [marr addObject:@(4)]; for (int i=0; i<[marr count]; i++) { NSLog(@"%@",marr[i]); } NSLog(@"%@",marr); } }
int main(int argc, const char * argv[]) { @autoreleasepool { /*不可变词典*/ NSDictionary *dic=[NSDictionary dictionaryWithObjectsAndKeys:@"学生",@"1",@"教师",@"2", nil]; for (NSString *key in dic) { NSLog(@"key:%@ value:%@",key,[dic objectForKey:key]); } } }
int main(int argc, const char * argv[]) { @autoreleasepool { /*可变词典*/ //创建可变词典对象 NSMutableDictionary *mdic=[NSMutableDictionary dictionary]; //添加键值对 [mdic setObject:@"学生" forKey:@"1"]; mdic[@"2"]=@"教师"; //显示键值对 NSLog(@"key:1 value is:%@",[mdic objectForKey:@"1"]);//key:1 value is:学生 NSLog(@"key:2 value is:%@",mdic[@"2"]);//key:2 value is:教师 //删除 [mdic removeObjectForKey:@"1"]; NSLog(@"mdic count:%lu",[mdic count]);//mdic count:1 [mdic removeAllObjects]; NSLog(@"mdic count:%lu",[mdic count]);//mdic count:0 } }
#import <Foundation/Foundation.h> @interface NSSet (Printing) -(void)print; @end
#import "NSSet+Printing.h" @implementation NSSet (Printing) -(void)print{ NSLog(@"------"); for(NSNumber *item in self){ NSLog(@"%li",(long)[item integerValue]); } } @end
#import <Foundation/Foundation.h> #import "NSSet+Printing.h" int main(int argc, const char * argv[]) { @autoreleasepool { //创建不可变集合 NSSet *set=[NSSet setWithObjects:@"1",@"3",@"5", @"7",nil]; //创建可变集合 NSMutableSet *mset=[NSMutableSet setWithObjects:@"1",@"2",@"3",@"4",@"5", nil]; //输出 [set print]; [mset print]; //成员测试 if([set containsObject:@"7"]==YES){ NSLog(@"set contain 7"); }else{ NSLog(@"set does not contain 7"); } //添加对象 [mset addObject:@"6"]; [mset print]; //移除对象 [mset removeObject:@"6"]; //求并集 [mset unionSet:set]; [mset print]; [mset print]; //求交集 [mset intersectSet:set]; [mset print]; } }
标签:
原文地址:http://www.cnblogs.com/janes/p/5467317.html