NSMutableString appendString 生成 SIGABRT
这对我来说毫无意义。也许这里有人可以解释为什么会发生这种情况。
我有一个 NSMutableString,我将其分配在 iPhone 应用程序的顶部,然后在稍后的过程中追加到其中。它会产生 SIGABRT,这对我来说并不重要。这是代码:
头文件(简化):
@interface MyAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
NSMutableString *locationErrorMessage;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, copy) NSMutableString *locationErrorMessage;
@end
以及主文件的相关部分:
@implementation MyAppDelegate
@synthesize window;
@synthesize locationErrorMessage;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
self.locationErrorMessage = [[NSMutableString alloc] init];
}
- (void)anotherFunction {
[self.locationErrorMessage appendString: @"Blah Blah Blah"];
}
这一切看起来都很简单。我缺少什么?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我将其称为属性设置器生成方式的错误,但答案非常简单:
您将属性声明为
(nonatomic, copy)
。这意味着每当设置locationErrorMessage
属性时,它都会对新值调用copy
并将该副本用作属性值。不幸的是,在
NSMutableString
上调用copy
不会产生NSMutableString
,它会产生一个NSString
(它不能使用诸如appendString:
之类的东西进行变异)。因此,简单的修复方法是将属性声明从
copy
更改为retain
。(我想说的错误是:如果您将可变对象的属性声明为copy,那么复制设置器实际上应该使用mutableCopy而不是copy ) =>雷达://8416047
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 thelocationErrorMessage
property is set, it's going to invokecopy
on the new value and use that copy as the property value.Unfortunately, invoking
copy
on anNSMutableString
does 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
copy
toretain
.(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 usemutableCopy
and notcopy
) => rdar://8416047您的属性正在复制传入的字符串。副本始终是不可变的,因此您尝试将
appendString:
发送到不可变的NSString
。将您的属性声明为retain
,它将起作用或编写一个自定义 setter,使用mutableCopy
复制字符串。您还存在内存泄漏,您应该使用
[NSMutableString string]
而不是alloc-init
序列。Your property is copying the passed in string. A copy always is immutable, so you’re trying to send
appendString:
to an immutableNSString
. Declare your property asretain
and it will work or write a custom setter that copies the string usingmutableCopy
.You also have a memory leak, you should use
[NSMutableString string]
instead of thealloc-init
sequence.顺便说一句,你那里有泄漏,
你正在复制该值,但你从未释放实际的第一个分配的 NSMutableString。
Btw, you have a leak there,
you're copying the value, but you never release the actual first allocated NSMutableString.