iOS學(xué)習(xí)(OC語言)知識點整理
一、NSSet、NSMutableSet集合的介紹
1)NSSet、NSMutableSet集合,元素是無序的,不能有重復(fù)的值。
2)用實例方法創(chuàng)建一個不可變集合對象 例如:
1 //宏定義2 #define TOBJ(n) [NSNumber numberWithInt:n]3 NSSet *set1=[[NSSet alloc]initWithObjects:TOBJ(2),TOBJ(3),TOBJ(3),TOBJ(1),TOBJ(5), nil];
2)用類方法創(chuàng)建一個不可變集合對象 例如:
1 NSSet *set2=[NSSet setWithObjects:TOBJ(2),TOBJ(4),TOBJ(6), nil];
3)NSSet 快速遍歷方法(無序,所以沒有下標(biāo))例如:
1 for(id num in set1){2 NSLog(@"%@",num);3 }
4)setSet 用于修改集合內(nèi)容 例如:[mSet setSet:set1];
5)intersectSet 用于獲取兩個集合的交集(返回兩個集合中相同的元素)。例如:
1 #define TOBJ(n) [NSNumber numberWithInt:n]2 NSSet *set1=[[NSSet alloc]initWithObjects:TOBJ(2),TOBJ(3),TOBJ(3),TOBJ(1),TOBJ(5), nil];3 NSSet *set2=[NSSet setWithObjects:TOBJ(2),TOBJ(4),TOBJ(6), nil];4 [mSet intersectSet:set2];5 NSLog(@"intersect:%@",mSet); //結(jié)果:2
6)unionSet 用于獲取兩個集合的并集(返回兩個集合中所有的元素,如果重復(fù)只顯示其中一個) 例如:
1 #define TOBJ(n) [NSNumber numberWithInt:n]2 NSSet *set1=[[NSSet alloc]initWithObjects:TOBJ(2),TOBJ(3),TOBJ(3),TOBJ(1),TOBJ(5), nil];3 NSSet *set2=[NSSet setWithObjects:TOBJ(2),TOBJ(4),TOBJ(6), nil];4 [mSet intersectSet:set2];5 NSLog(@"intersect:%@",mSet); //結(jié)果:123456
7)minusSet 用于獲取兩個集合的差集 例如:
1 #define TOBJ(n) [NSNumber numberWithInt:n]2 NSSet * mSet =[[NSSet alloc]initWithObjects:TOBJ(2),TOBJ(3),TOBJ(3),TOBJ(1),TOBJ(5), nil];3 NSSet *set2=[NSSet setWithObjects:TOBJ(2),TOBJ(4),TOBJ(6), nil];4 [mSet minusSet:set2];5 NSLog(@"intersect:%@",mSet); //結(jié)果:13456
8)allObjects 用于將集合轉(zhuǎn)換為數(shù)組 例如:
1 #define TOBJ(n) [NSNumber numberWithInt:n]2 NSSet * mSet =[[NSSet alloc]initWithObjects:TOBJ(2),TOBJ(3),TOBJ(3),TOBJ(1),TOBJ(5), nil];3 NSArray *array= [mSet allObjects];
9)anyObject 取set中任意一個元素(如果set中只有一個元素,取值)
1 #define TOBJ(n) [NSNumber numberWithInt:n]2 NSSet * mSet =[[NSSet alloc]initWithObjects:TOBJ(2),TOBJ(3),TOBJ(3),TOBJ(1),TOBJ(5), nil];3 id value=[mSet anyObject];
二、NSIndexSet、NSMutableIndexSet 可變索引集合的介紹
1)索引集合,表示唯一的整數(shù)的集合,有可變和不可變之分。
2)initWithIndexesInRange 用指定的范圍對應(yīng)的索引創(chuàng)建索引對象 例如:
1 NSIndexSet *indexSet1=[[NSIndexSet alloc]initWithIndexesInRange:2 NSMakeRange(2, 3)];//結(jié)果 2,3,4
3)objectsAtIndexes 根據(jù)索引集合中的索引取出數(shù)組中對應(yīng)的元素(返回數(shù)組) 例如:
1 NSIndexSet *indexSet1=[[NSIndexSet alloc]initWithIndexesInRange:NSMakeRange(2, 3)]; 2 NSArray *array=@[@"one",@"two",@"three",@"four",@"five",@"sex"];3 NSArray *array2=[array objectsAtIndexes:indexSet1];4 NSLog(@"array2:%@",array2); //結(jié)果:array2:three four five
4)創(chuàng)建一個可變的集合索引(初始化時有一個索引)(可以存儲不連續(xù)的索引值) 例如:
1 NSMutableIndexSet *indexSet2=[NSMutableIndexSet indexSetWithIndex:2];2 [indexSet2 addIndex:4];3 [indexSet2 addIndex:1];4 [indexSet2 addIndex:2];5 NSLog(@"count:%ld",indexSet2.count);//獲取個數(shù)6 NSArray *array3=[array objectsAtIndexes:indexSet2];//結(jié)果:two three five
5)NSNull:類表示空, 只有一個類方法[NSNull null]獲取空對象,在數(shù)組中nil表示元素結(jié)束(不能用nil表示空元素
可采用[NSNull null]表示空元素) 例如:
1 NSArray *array5=[NSArray arrayWithObjects:@"red",[NSNull null],@"yellow",@"blue", nil];
三、Category 介紹
1)Category 意為: 類別、分類、類目
1、可以在不改變類名的情況下,擴(kuò)充類的功能(給類增加方法)
2、可以將類的功能拆成多個文件編譯
3、類別中不能增加成員變量,可以訪問原來類中的成員變量
4、類別中可以增加與原來類中同名的方法,調(diào)用時優(yōu)先調(diào)用
5、添加文件時選擇Objective-C File 那個文件同時注意選擇要拓展的類名
2)類別的聲明類似于類的聲明,@interface要擴(kuò)充功能的類名(類別名)
1、類別不能實例化對象
2、類別中不能增加成員變量。
3、類別中的方法可以訪問原來類中的成員變量
4、類別可以調(diào)用原來類中的方法
5、類別中的方法可以被子類繼承
6、類別可以添加與原來類中相同的方法,調(diào)用時類別中的方法優(yōu)先調(diào)用,一般不建議這樣操作(無法再調(diào)用原來類中的方法)
3)字符串、NSNumber是簇類,底層是由很多類組成的,不能有子類 ,因為子類調(diào)用不了父類中的方法
4)Category 文件名格式為:父類文件名+子類文件名 如:NSMutaleString+Resvrse.h
5)Category .m文件中的方法表現(xiàn)形式: 1 @implementation NSMutableString (Reverse)
四、Extension 的介紹
1)extension:相當(dāng)于未命名的Category,可以擴(kuò)展類的功能(增加方法),也可以增加成員變量。
2)extension:只有.h文件
3)extension 表現(xiàn)形式 @interface 類名()例如: 1 @interface Person (){}
4)在.m文件中也可以聲明成員變量,不會將其放在接口h文件中暴露給使用者。 例如:
1 @interface Person()2 { 3 int _num;4 }5 //將方法聲明為私有的6 -(void)PRint2;7 @end
五、SEL的介紹
1)SEL是一種類型,將方法名封裝為sel的變量,通過SEL找到方法的地址,調(diào)用方法。
2)SEL 封裝方法實例代碼:
1 //將play方法名封裝成SEL類型的數(shù)據(jù)2 SEL sel=@selector(play);3 //判斷p1所屬的類是否實現(xiàn)了sel中的方法4 if([p1 respondsToSelector:sel]){5 //p1查找sel中方法的地址,再調(diào)用對應(yīng)的方法6 [p1 performSelector:sel];7 }
3)使用SEL 方法實現(xiàn)數(shù)組排序 實例代碼:
1、創(chuàng)建一個Student類 .h文件中的代碼如下:
1 #import <Foundation/Foundation.h> 2 @interface Student : NSObject { 3 NSString *_name; 4 int _age; 5 int _score; 6 } 7 8 -(void)setName:(NSString*)name; 9 -(NSString *)name;10 -(void)setAge:(int)age;-(int)age;11 -(void)setScore:(int)score;-(int)score;12 -(NSComparisonResult)compareStudent:(Student *)s2;13 +(Student *)studentWithName:(NSString *)n andAge:(int)a andScore:(int)s;14 -(void)print;15 @end
2、.m文件中的代碼實現(xiàn)
1 #import "Student.h" 2 @implementation Student 3 -(void)setName:(NSString*)name{ 4 if(name!=_name){ 5 [_name release]; 6 _name=[name retain]; 7 } 8 } 9 -(NSString *)name{ return _name;}10 -(void)setAge:(int)age{ 11 _age=age;12 }13 -(int)age{ 14 return _age;15 }16 -(void)setScore:(int)score{ 17 _score=score;18 }19 -(int)score{20 return _score;21 }22 +(Student *)studentWithName:(NSString *)n andAge:(int)a andScore:(int)s{ 23 Student *stu=[[[Student alloc]init]autorelease]; 24 stu.name=n; 25 stu.age=a; 26 stu.score=s; 27 return stu;28 }29 -(void)print{ 30 NSLog(@"My Name Is %@ Age Is %d Score Is %d",_name,_age,_score);31 }32 -(NSComparisonResult)compareStudent:(Student *)s2{ if(self.score<s2.score){ 33 return NSOrderedAscending; 34 }else if (self.score>s2.score){ 35 return NSOrderedDescending; 36 }else{ 37 if(self.age<s2.age){ 38 return NSOrderedAscending; 39 }else if (self.age>s2.age){ 40 return NSOrderedDescending; 41 }else{ 42 return [self.name compare:s2.name]; 43 } 44 }45 }46 -(void)dealloc{ 47 NSLog(@"student %@ dealloc",_name); 48 [_name release]; 49 [super dealloc];50 }51 @end
3、main文件中的執(zhí)行代碼
1 NSMutableArray *array=[[NSMutableArray alloc]init]; 2 Student *stu1=[Student studentWithName:@"zhangsan" andAge:20 andScore:90]; 3 Student *stu2=[Student studentWithName:@"lisi" andAge:20 andScore:90]; 4 Student *stu3=[Student studentWithName:@"wangwu" andAge:22 andScore:90]; 5 Student *stu4=[Student studentWithName:@"zhaoliu" andAge:20 andScore:80]; 6 Student *stu5=[Student studentWithName:@"tom" andAge:19 andScore:80]; 7 [array addObject:stu1]; 8 [array addObject:stu2]; 8 [array addObject:stu3]; 9 [array addObject:stu4]; 10 [array addObject:stu5]; 11 [array sortUsingSelector:@selector(compareStudent:)]; 12 for(Student *s in array){ 13 [s print]; 14 } 15 [array release];
4)performSelector 用于執(zhí)行SEL封裝的方法 例如: 1 [p1 performSelector:@selector(jump)];
5)SEL 封裝帶參數(shù)的方法實例代碼:
1 //將帶一個參數(shù)的方法封裝為SEL的變量,執(zhí)行,參數(shù)是id類型2 [p1 performSelector:@selector(print:) withObject:@"hello"];
6)NSSelectorFromString 用于將字符串形式的方法名封裝成SEL的數(shù)據(jù) 實例代碼:
1 SEL sel2=NSSelectorFromString(@"study");2 [p1 performSelector:sel2];
7)_cmd 表示當(dāng)前執(zhí)行的方法 例如: 1 NSLog(@"*****metheod:%@",NSStringFromSelector(_cmd));
8)在C語言中 __func__ 表示獲取當(dāng)前執(zhí)行方法 例如: 1 NSLog(@"func=%s",__func__);
__DATE__ 表示獲取當(dāng)前系統(tǒng)時間 1 NSLog(@"date=%s",__DATE__);
9)SEL實現(xiàn)數(shù)組排序 實例代碼:
1 void testSel()2 {3 Person *p1;4 Person *p2;5 Person *p3;6 NSMutableArray *array1=[[NSMutableArray alloc]initWithObjects:p1,p2,p3, nil];7 [array1 sortUsingSelector:@selector(comparePerson:)];8 9 }
六、構(gòu)造OC中的二維數(shù)組 實例代碼:
//創(chuàng)建一個空的外層數(shù)組 2 NSMutableArray *bigArray=[NSMutableArray array]; 3 //創(chuàng)建一個存放4個數(shù)據(jù)對象的數(shù)組 4 NSMutableArray *array1=[[NSMutableArray alloc]init]; 5 for(int i=0;i<4;i++){ 6 [array1 addObject:[NSNumber numberWithInt:i]]; 7 } 8 //創(chuàng)建一個存放3個字符串的數(shù)組 9 NSMutableArray *array2=[[NSMutableArray alloc]init];10 for(int i=0;i<3;i++){11 [array2 addObject:[NSString stringWithFormat:@"str%d",i+1]];12 }13 //將array1和array2兩個數(shù)組對象存入外層數(shù)組(相當(dāng)于創(chuàng)建了一個二維數(shù)組)14 [bigArray addObject:array1];15 [bigArray addObject:array2];16 17 //遍歷,顯示所有的元素18 for(int i=0;i<bigArray.count;i++){19 for(int j=0;j<[bigArray[i] count];j++){20 //取出數(shù)組中第i行第j列的元素(每行又是一個數(shù)組對象)21 if([bigArray[i][j] isKindOfClass:[NSNumber class]]){22 NSLog(@"number:%@",bigArray[i][j]);23 }24 else if ([[[bigArray objectAtIndex:i] objectAtIndex:j] isKindOfClass:[NSString class]]){25 NSLog(@"string:%@",[[bigArray objectAtIndex:i] objectAtIndex:j]);26 }27 }28 }
七、Class (類)的介紹
1)類的本質(zhì)也是一個對象,是Class類型的對象,獲取類對象(可以通過實例方法或類方法獲?。?
每個類只有一個類對象。
2)load 方法當(dāng)程序啟動時會加載所有的類和分類,調(diào)用load方法,先加載父類,再加載子類,然后是分類 例如:
1 +(void)load2 {3 NSLog(@"Person---load");4 }
3)initialize方法 當(dāng)?shù)谝淮问褂妙惖臅r候,調(diào)用initialize方法,先調(diào)用父類的,再調(diào)用子類的 例如:
1 +(void)initialize2 {3 NSLog(@"Person---initialize");4 }
新聞熱點
疑難解答