iOS Developer

Objective-C类和对象

2016-07-10  本文已影响29人  5c0d26b96912

1.常用对象

NSString  *str = [NSString stringWithFormat : (NSString *) format,…]  //字符串
NSMutableString  *ms = [NSMutableString stringWithCapacity : numItems]  //可变字符串
NSArray *arr = [NSArray arrayWithObjects : @“abc”, @“123”, nil]  //数组
NSMutableArray *ma = [NSMutableArray arrayWithCapacity : numItems]  //可变数组
NSEnumerator *enum = [array objectEnumerator]  //枚举
NSEnumerator *enum1 = [array reverseObjectEnumerator] //反相枚举
for(NSString *str in array)  //快速枚举
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys : (NSObject *)obj, key,…]  //字典
NSMutableDictionary *md = [NSMutableDictionary dictionaryWithCapacity : numItems]  //可变字典
NSDictionaryEnumerator *me =  [NSDictionaryEnumerator enumeratorAtPath: value]  //字典枚举
NSNumber *num = [NSNumber numberWith* : value]    char int float bool string    //数
NSValue  *value = [NSValue valueWith* : value, type]    //值
NSNull    //空类
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]  [pool release]  //自动释放池

2.对象生命周期

3.引用计数

Cocoa采用了一种引用计数。每个对象有一个与之相关的证书,称作引用计数器或者保留计数器。

-(id) retain;  //计数器+1
-(void) release;    //计数器-1
-(unsigned) retainCount;    //获取计数值
init //初始化时调用
dealloc //释放时调用
autorelease //自动释放时调用

对象初始化

 Car *car1 = [Car new];```
上一篇 下一篇

猜你喜欢

热点阅读