您好是一个开源项目的代码:iOS中__weak和__strong attibutes之间的区别是什么?
- (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;
}
我想知道当一个类需要设置self
强还是弱?
我不认为代码实际上是正确的。由于没有保留块,所以不需要使用'weakSelf'。 –
@CarlVeazey该项目位于https://github.com/kolyvan/kxmovie,它运行良好,但在将代码复制(合并)到其他项目时无法工作 – why
理解强与弱是目标中相当重要的概念-C以及在任何课程/书籍中很早就涵盖的内容。如果你的问题是特定于代码的(“我理解强,弱,但我不明白它是如何用在这个代码中的)”,请重新表述清楚。如果你在寻找一般的理解,我会建议阅读一个Objective-C的参考文献,因为它对你所有未来的代码都很重要。此外,这个问题已经在这里介绍:http://stackoverflow.com/questions/9262535/explanation-of-strong-and-weak-storage-in-ios5 –