object C基础
星期三, 2013-01-16 | Author: liweizhou40 | ios | 3,185 views
这篇主要记录学习apple developer上的Programming with Objective-C的体会
1、类
类是创建对象的模板,接口是类对外的展示
(1)接口定义
1 2 3 4 5 6 7 | @interface Person : NSObject @property (readonly) NSString *firstName; @property (readonly) NSString *lastName; @property int yearOfBirth; - (void)sayHello; - (void)saySomething:(NSString *)someWords; @end |
需要注意,名称必须唯一,所以一般会添加前缀,防止命名冲突(比如ZLWPerson)。
在方法前加-,代表这是一个实例方法。类方法前加+。
(2)类定义
1 2 3 4 5 6 7 8 9 10 | #import "XYZPerson.h" @implementation XYZPerson - (void)sayHello { [self saySomething:@"Hello, world!"]; } - (void)saySomething:(NSString *)someWords{ NSLog(@"%@", someWords); } @end |
如果要调用父类的方法[super saySomething:someWords];
(3)对象
方法调用
1 | [zhoulw saySomething:@"shut up!"]; |
初始化(可带参数)
1 | SomeObject *newObject = [[SomeObject alloc] initWithInt:30]; |
利用new初始化(不带参数,只是简化写法)
1 | SomeObject *newObject = [SomeObject new]; |
利用工厂方法(实际上还是调用了初始化)
1 | NSNumber *magicNumber = [NSNumber numberWithInt:42]; |
利用字面量
1 | NSString *name = @"zlw"; |
nil:不同于java的null,不会抛异常
1 2 3 | if (somePerson) { // somePerson points to an object } |
实例变量
1 2 3 4 5 6 | @synthesize firstName = ivar_firstName; ... NSString* firstName = [personZlw firstName]; firstName = personZlw.firstName; firstName = ivar_firstName; [personZlw setFirstName:@"zhoulw"]; |
没有使用@synthesize,则实例变量名为_firstName;
@synthesize firstName,则实例变量名就是firstName;
可以不写property,定义实例变量,不过不建议使用;
在init方法中,直接使用实例变量赋值,而不用set方法(why?没有实例?);
get,set方法是原子操作
强引用,弱引用
1 2 | @property (weak) id delegate; NSObject * __weak weakVariable; |
有些类不可以被弱引用,如NSTextView, NSFont and NSColorSpace
1 2 | @property (unsafe_unretained) NSObject *unsafeProperty; NSObject * __unsafe_unretained unsafeReference; |
copy修饰
1 2 3 4 5 6 | NSMutableString *nameString = [NSMutableString stringWithString:@"John"]; self.badgeView.firstName = nameString; [nameString appendString:@"ny"]; ... //直接赋值时别忘记copy _instanceVariableForCopyProperty = [aString copy]; |
4、protocol
略,原来写了,忘记保存,懒得补
5、block
1 2 3 4 5 | void (^simpleBlock)(void) = ^{ NSLog(@"This is a block"); }; ... simpleBlock(); |
1 2 3 4 5 6 | double (^multiplyTwoValues)(double, double) = ^(double firstValue, double secondValue) { return firstValue * secondValue; }; double result = multiplyTwoValues(2,4); NSLog(@"The result is %f", result); |
1 2 3 4 5 6 | int anInteger = 42; void (^testBlock)(void) = ^{ NSLog(@"Integer is: %i", anInteger); }; anInteger = 84; testBlock(); //42 |
__block block可以知道变量修改,并且block可以改变变量值
1 2 3 4 5 6 7 8 | __block int anInteger = 42; void (^testBlock)(void) = ^{ NSLog(@"Integer is: %i", anInteger); anInteger = 100; }; anInteger = 48; testBlock();//48 NSLog(@"Value of original variable is now: %i", anInteger);//100 |
1 2 3 4 5 6 7 8 | typedef void (^XYZSimpleBlock)(void); XYZSimpleBlock anotherBlock = ^{ ... }; - (void)beginFetchWithCallbackBlock:(XYZSimpleBlock)callbackBlock { ... callbackBlock(); } |
文章作者: liweizhou40
本文地址: https://www.pomelolee.com/1104.html
除非注明,Pomelo Lee文章均为原创,转载请以链接形式标明本文地址
No comments yet.
Leave a comment
Search
相关文章
热门文章
最新文章
文章分类
- ajax (10)
- algorithm-learn (3)
- Android (6)
- as (3)
- computer (85)
- Database (30)
- disucz (4)
- enterprise (1)
- erlang (2)
- flash (5)
- golang (3)
- html5 (18)
- ios (4)
- JAVA-and-J2EE (186)
- linux (143)
- mac (10)
- movie-music (11)
- pagemaker (36)
- php (50)
- spring-boot (2)
- Synology群晖 (2)
- Uncategorized (6)
- unity (1)
- webgame (15)
- wordpress (33)
- work-other (2)
- 低代码 (1)
- 体味生活 (40)
- 前端 (21)
- 大数据 (8)
- 游戏开发 (9)
- 爱上海 (19)
- 读书 (4)
- 软件 (3)