一、点语法本质
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 及使用细节
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 方法的执行过程、自定义)
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创建Student类对象,[stu class]是获取内存中的类对象3 Class stu2 = [Student class]; //stu1的地址等于stu2的地址,都是stu的地址
1 + (void)load {2 //程序一启动,所有的类都调用这个加载方法3 }
1 + (void)initialize {2 //第一次使用类的时候([[类 alloc]init]),就会调用一次这个方法。我们可以在这里监听类何时被使用3 }
九、description 方法
1 - (NSSting *)description {2 // NSLog(@"%@",self); //这行代码会引发死循环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 }
新闻热点
疑难解答