简单的说一下非原子性的nonatomic实现
- (void)setCurrentImage:(UIImage *)currentImage
{
if (_currentImage != currentImage) {
[_currentImage release];
//如果是nsstring类型,则是copy
_currentImage = [currentImage retain];
// do something
}
}
- (UIImage *)currentImage
{
if (_currentImage == nil)
{
_currentImage = [[UIImage alloc]init] ;
}
return _currentImage;
}
atomic实现:
- (void)setCurrentImage:(UIImage *)currentImage
{
@synchronized(self) {
if (_currentImage != currentImage) {
[_currentImage release];
_currentImage = [currentImage retain];
// do something
}
}
}
- (UIImage *)currentImage
{
@synchronized(self) {
UIImage *image = [_currentImage retain];
return [image autorelease];
}
}