This makes no sense to me. Maybe someone here can explain why this happens.
I’ve got an NSMutableString that I alloc at the top of my iPhone app, then append to later in the process. It results in a SIGABRT, which doesn’t add up to me. Here’s the code:
Header File (simplified):
@interface MyAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
NSMutableString *locationErrorMessage;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, copy) NSMutableString *locationErrorMessage;
@end
And the relevant parts of the Main:
@implementation MyAppDelegate
@synthesize window;
@synthesize locationErrorMessage;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
self.locationErrorMessage = [[NSMutableString alloc] init];
}
- (void)anotherFunction {
[self.locationErrorMessage appendString: @"Blah Blah Blah"];
}
This all seems simple enough. What am I missing?
I would call this a bug in how property setters are generated, but the answer is pretty simple:
You declared the property as
(nonatomic, copy). This means that whenever thelocationErrorMessageproperty is set, it’s going to invokecopyon the new value and use that copy as the property value.Unfortunately, invoking
copyon anNSMutableStringdoes not result in anNSMutableString, it results in anNSString(which cannot be mutated using something likeappendString:).So the simple fix would be to change the property declaration from
copytoretain.(I would say that the bug would be: If you declare a property for a mutable object as
copy, then the copy setter should actually usemutableCopyand notcopy) => rdar://8416047