
比如:
+ (instancetype)sharedManager { static PhotoManager *sharedPhotoManager = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ [NSThread sleepForTimeInterval:2]; sharedPhotoManager = [[PhotoManager alloc] init]; NSLog(@"Singleton has memory address at: %@", sharedPhotoManager); [NSThread sleepForTimeInterval:2]; sharedPhotoManager->_photosArray = [NSMutableArray array]; }); return sharedPhotoManager; } 这里有:
static PhotoManager *sharedPhotoManager = nil; static dispatch_once_t onceToken; 为何用static修饰?不用staitc有何影响?
1 cheng4741 2015 年 7 月 28 日 不用static,下次执行这函数怎么知道执行过没有? local Variable 执行完函数就销毁了。成员变量也不行,这是类方法。所以只能是static |
2 loveuqian 2015 年 7 月 28 日 via iPhone onceToken 顾名思义 |
3 loveuqian 2015 年 7 月 28 日 via iPhone 你可以去看下培训班视频。ui进阶第一天的视频里面有很详细讲,包括app启动原理,uiwindow,我昨天上的就是这个。。。 |
4 xuyuheng0905 2015 年 7 月 28 日 这叫local-static变量,函数第一次调用时会被创建,并初始化为0,之后生命周期跟应用生命周期保持一致。 |