NSMutableString appendString 生成 SIGABRT

发布于 2024-09-19 05:04:20 字数 831 浏览 8 评论 0原文

这对我来说毫无意义。也许这里有人可以解释为什么会发生这种情况。

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

追风人 2024-09-26 05:04:20

我将其称为属性设置器生成方式的错误,但答案非常简单:

您将属性声明为 (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 the locationErrorMessage property is set, it's going to invoke copy on the new value and use that copy as the property value.

Unfortunately, invoking copy on an NSMutableString does not result in an NSMutableString, it results in an NSString (which cannot be mutated using something like appendString:).

So the simple fix would be to change the property declaration from copy to retain.

(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 use mutableCopy and not copy) => rdar://8416047

表情可笑 2024-09-26 05:04:20

您的属性正在复制传入的字符串。副本始终是不可变的,因此您尝试将 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 immutable NSString. Declare your property as retain and it will work or write a custom setter that copies the string using mutableCopy.

You also have a memory leak, you should use [NSMutableString string] instead of the alloc-init sequence.

淡淡の花香 2024-09-26 05:04:20

顺便说一句,你那里有泄漏,

self.locationErrorMessage = [[NSMutableString alloc] init];

你正在复制该值,但你从未释放实际的第一个分配的 NSMutableString。

Btw, you have a leak there,

self.locationErrorMessage = [[NSMutableString alloc] init];

you're copying the value, but you never release the actual first allocated NSMutableString.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文