首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > 移动开发 >

Objective-C 中一些代码记要[转]

2012-06-29 
Objective-C 中一些代码记录[转]http://unmi.cc/objective-c-snippets1.?初始化一个空的数组??? NSMutable

Objective-C 中一些代码记录[转]

http://unmi.cc/objective-c-snippets

1.?初始化一个空的数组

??? NSMutableArray *array = [NSMutableArray arrayWithObjects:nil];
?? ?
??? //或者,这里的?Capacity?像 java?的 ArrayList?中的 Capacity
??? //NSMutableArray ×array = [NSMutableArray arrayWithCapacity:5];
? ?
??? MSLog(@"%i", [array count]);

如果用到了 alloc?的话,就必须自己处理好相应的 release?操作了,像:

??? NSMutableArray *array = [[NSMutableArray alloc] init];
??? NSMutableArray *array1 = [[NSMutableArray alloc] initWithCapacity:5];
??? NSMutableArray *array2 = [[NSMutableArray alloc]initWithObjects:nil];

?

其实要初始化某种类型集合的空集合,下面的那些方式应该是更为合适的:

? ? NSArray *array = [NSArray array];

? ? NSMutableArray *array1 = [NSMutableArray array];

? ? NSMutableDictionary *dict = [NSMutableDictionary dictionary];

? ? NSSet *set = [NSSet set];

2.?类的初始化方法:

}

执行结果输出中顺序是不定的,像:

011-08-09 14:20:54.144 TestObjC[3602:1c03] Thread Two Start run: 2011-08-09 06:20:54 +0000
2011-08-09 14:20:54.144 TestObjC[3602:1e03] Thread One Start run: 2011-08-09 06:20:54 +0000
2011-08-09 14:20:54.146 TestObjC[3602:1e03] Thread Three Start run: 2011-08-09 06:20:54 +0000

NSOperation?还有一个子类是? NSInvocationOperation,它与? NSOperation?的区别是可以指定线程要执行的实例的某个方法,而不只限制是 main?方法。

热点排行