Objective-C 中一些代码记录

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. 类的初始化方法:
 1NSInteger globalVar = 5;
 2
 3@interface TestClass : NSObject
 4- (void) foo;
 5@end
 6
 7@implementation TestClass
 8
 9//在第一次加载 TestClass 时被自动调用
10+ (void) initialize {
11    extern NSInteger globalVar;
12    globalVar += 5;
13}
14
15- (void) foo {
16    NSLog(@"globalVar: %i", globalVar);
17}
18
19@end
20
21    //应用上面的代码
22    TestClass *test = [[TestClass alloc] init];
23    [test foo]; //输出为 10
24    TestClass *test1 = [[TestClass alloc] init];
25    [test1 foo]; //输出也是 10

Objective-C 的 + (void) initialize 就相当于 Java 中的 static {}  静态块一样,+ (void) initialize 类初始方法也只会被调用一次。在 Objective-C 和 Java 它们各自反应为:

Objective-C 的   + (void) initialize   ------------    _class_initialize
Java 的                static  {}                   ------------   <cinit>,  现在看到的是 static{}

3. 多线程相关的代码

Obj-C 中与多线程相关的类有 NSOperation、NSOperationQueue 和 NSThread。NSOperation 类似与 Java 的 Runnable 接口,只是要实现的 NSOperation 的方法是 -(void) main; 当把 NSOperation 加到 NSOperationQueue 后,队列就会为每个 NSOperation 实例分配一个 NSThread 去启动它。NSOperation 执行完后会被 release 掉。

下面是使用 NSOperation 和 NSOperationQueue 的一段完整代码:
 1#import <Foundation/Foundation.h>
 2
 3@interface MyOperation : NSOperation{
 4    NSString *name;
 5}
 6
 7@end
 8
 9@implementation MyOperation
10
11- (id) initWithName: (NSString *) theName {
12    self = [super init];
13    name = theName;
14    return self;
15}
16
17- (void) main {
18    NSLog(@"Thread %@ Start run: %@", name, [NSDate date]);
19}
20
21@end
22
23int main (int argc, const char * argv[])
24{
25
26    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
27
28    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
29    NSOperation *operation1 = [[[MyOperation alloc] initWithName:@"One"] autorelease];
30    NSOperation *operation2 = [[[MyOperation alloc] initWithName:@"Two"] autorelease];
31    NSOperation *operation3 = [[[MyOperation alloc] initWithName:@"Three"] autorelease];
32    [queue addOperation:operation1];
33    [queue addOperation:operation2];
34    [queue addOperation:operation3];
35
36    [queue setMaxConcurrentOperationCount:2];//可设置同时并发数
37
38    sleep(50000);
39    [pool drain];
40    return 0;
41}

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

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 方法。 永久链接 https://yanbin.blog/objective-c-snippets/, 来自 隔叶黄莺 Yanbin's Blog
[版权声明] 本文采用 署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0) 进行许可。