【发布时间】:2013-05-29 13:57:06
【问题描述】:
我只是想知道下面第 1 行和第 2 行的区别:
_subtitle = @"Test"; //Line 1
_subtitle = [NSString stringWithFormat: @"Test"]; //Line 2
如果我问这个问题,那是因为我在使用 MKAnnotation 时遇到了问题。在下面的方法中,我尝试更新 MKAnnotation 的字幕委托属性(它是非原子的、复制的和只读的)。但是看起来我在使用第 2 行时得到了一个僵尸,而在使用第 1 行时却没有。所以我的问题是为什么?
- (void) initCoordinateWithAddress:(NSString*)address;
{
self.address = address;
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString: address completionHandler:^(NSArray *placemarks,NSError *error)
{
CLPlacemark *place = [placemarks objectAtIndex:0];
_coordinate = place.location.coordinate;
_title = self.address;
_subtitle = @"Test"; //Line 1: don't crash
_subtitle = [NSString stringWithFormat: @"Test"]; //Line 2: crash
//_subtitle = [[NSString stringWithFormat: @"[%.2f,%.2f]", self.coordinate.latitude, self.coordinate.longitude] copy];
_isInit = YES;
[self.customDelegate didCalculateCoordinate: place.location.coordinate forAnnotation: self];
}];
}
我实际上已经通过使用方法复制解决了我的问题,但我仍然不明白第 1 行和第 2 行之间有什么区别,如果有人可以帮助我了解区别是什么,我将不胜感激。
编辑:
1- 我没有使用 ARC
2- _subtitle 来自@synthesize subtitle = _subtitle;而 subtitle 是 MKAnnotation 协议的一部分,具有 nonatomic、readonly 和 copy 属性
问候, 西里尔
【问题讨论】:
-
我认为这个答案适合这个问题:stackoverflow.com/a/4154419/883799
-
您可以查看这个不错的答案:stackoverflow.com/questions/8275131/…
-
最后你在一个块中执行这个代码,这可能会导致问题。
-
这段代码是使用ARC还是手动引用计数?还有,
_subtitle的声明是什么? -
感谢您的回答。所以 _subtitle 来自 @synthesize subtitle = _subtitle; subtitle 是 MKAnnotation 协议的一部分,具有 nonatomic、readonly 和 copy 属性。而且,我没有使用 ARC。
标签: ios objective-c nsstring mkannotation stringwithformat