why*_*why 4 weak-references objective-c strong-references ios6
有一个开源项目中的代码:
- (id) initWithContentPath: (NSString *) path parameters: (NSDictionary *) parameters
{
NSAssert(path.length > 0, @"empty path");
playPath = path;
self = [super initWithNibName:nil bundle:nil];
if (self) {
_moviePosition = 0;
_startTime = -1;
self.wantsFullScreenLayout = YES;
_decodeDuration = DEFAULT_DECODE_DURATION;
_minBufferedDuration = LOCAL_BUFFERED_DURATION;
_parameters = parameters;
__weak KxMovieViewController *weakSelf = self;
dispatch_async(dispatch_get_global_queue(0, 0), ^{
NSError *error;
KxMovieDecoder *decoder;
decoder = [KxMovieDecoder movieDecoderWithContentPath:path error:&error];
NSLog(@"KxMovie load video %@", path);
__strong KxMovieViewController *strongSelf = weakSelf;
if (strongSelf) {
dispatch_sync(dispatch_get_main_queue(), ^{
[strongSelf setMovieDecoder:decoder withError:error];
});
}
});
}
return self;
}
Run Code Online (Sandbox Code Playgroud)
我想知道一个班什么时候需要设置self为强或弱?
一个强大的,当你要确保你所引用,而你仍然在使用它不释放对象引用时使用。一个弱引用时使用你,如果你引用的对象被释放不在乎。nil当没有对该对象的强引用时,会自动设置弱引用。
基本上,只要至少有一个对象的强引用,它就不会被释放。当没有更多强引用时,所有弱引用(如果有)都设置为nil。
| 归档时间: |
|
| 查看次数: |
2899 次 |
| 最近记录: |