我只是想知道下面第 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 属性
问候,西里尔