一、點語法本質
1 //方法調用2 Student *stu = [[Student alloc] init]; 3 [stu setAge:10]; 4 int age = [stu age];5 //-----------------------------我是華麗分割線-----------------------------6 //點語法 7 stu.age = 10; 8 int age = stu.age;
二、成員變量的作用域
三、@property 和 @synthesize 、setter 和 getter 及使用細節(jié)
1 //--[interface.h]---Xcode4.2之前的語法---------------我是華麗分割線-------- 2 @property int age; //@property 3 //--[interface.h]--------??等價于??-------- 4 - (void)setAge; 5 - (int)age; 6 7 //--[implementation.m]-------------------------------我是華麗分割線-------- 8 @synthesize int age = _age; //@synthesize 9 //--[implementation.m]---??等價于??--------10 - (void)setAge {11 _age = age;12 }13 - (int)age {14 return _age;15 }16 //--[implementation.m]-------------------------------我是華麗分割線--------17 @synthesize int age; //@synthesize18 //--[implementation.m]---??等價于??--------19 - (void)setAge {20 _age = age;21 }22 - (int)age {23 return age;24 }25 26 //--[interface.h]---Xcode4.4之后有了以下新語法-------我是華麗分割線-------27 @property int age; //@property28 //--[interface.h]---------??等價于??-------29 @interface Student:NSObject{30 int _age;31 }32 - (void)setAge;33 - (int)age; 34 //--[implementation.m]---------------------35 - (void)setAge {36 _age = age;37 }38 - (int)age {39 return _age;40 }
四、id
1 typedef struct objc_object {2 Class isa; //每個對象都有一個isa,且isa始終指向當前類本身3 } *id; // id 定義為一個結構指針
五、構造方法(基本概念、重寫 init 方法、init 方法的執(zhí)行過程、自定義)
1 //------NSObject------------2 - (id)init {3 isa = [self class];4 return slef;5 }
六、更改 Xcode 模版(main.m 、注釋)
七、分類(基本使用、使用注意、給 NSString 增加類方法及擴充對象方法)
八、類的深入研究(本質、類對象的使用、類的加載和初始化)
1 Student *stu = [[Student alloc] init];2 Class stu1 = [stu class]; //利用Class創(chuàng)建Student類對象,[stu class]是獲取內存中的類對象3 Class stu2 = [Student class]; //stu1的地址等于stu2的地址,都是stu的地址
1 + (void)load {2 //程序一啟動,所有的類都調用這個加載方法3 }
1 + (void)initialize {2 //第一次使用類的時候([[類 alloc]init]),就會調用一次這個方法。我們可以在這里監(jiān)聽類何時被使用3 }
九、description 方法
1 - (NSSting *)description {2 // NSLog(@"%@",self); //這行代碼會引發(fā)死循環(huán)3 return [NSString stringWithFormat:@"age=%d, name=%@", _age, _name];4 }
十、NSLog 輸出補充
1 int main() {2 NSLog(@"%d",__LINE__); //輸出當前行號(即 2 )3 //NSLog(@"%s",__FILE__); //NSLog輸出 C 語言字符串的時候,不能有中文4 printf(@"%s/n",__FILE__); //輸出源文件的名稱(含路徑)5 NSLog(@"%s/n",__func__); //輸出當前函數名(即 main )6 }
十一、SEL (基本用法及其他使用)
1 int main() {2 Student *stu = [[Student alloc] init];3 [stu test]; 4 [stu performSelector:@selector(test)]; //間接調用test方法,@selector(test)就是一個SEL類型5 [stu performSelector:@selector(test1:) withObject:@"123"]; //間接調用test:方法,@selector(test:)就是一個SEL類型6 }
1 NSString *name = @"test";2 SEL s = NSSelectorFromSrting(name) //將test方法包裝成SEL數據3 [stu performSelector:s];
1 - (void)test {2 NSString *str = NSStingWithSelector(_cmd);3 NSLog(@"調用了test方法---%@",str); //顯示:調用了test方法---test4 }
新聞熱點
疑難解答