stringByAppendingString 和保留计数的问题

发布于 2024-11-08 13:57:41 字数 583 浏览 0 评论 0原文

我需要连接一些字符串。

NSString * sportName = [[NSString alloc ]initWithString:@""];
sportName = [sportName stringByAppendingString:[someObject getString]];
// can be more stringByAppendingString
...
[sportName release];

但对我来说有些奇怪的是给出了“构建和分析”命令:

在字符串上:

sportName = [sportName stringByAppendingString:[someObject getString]]; 

错误: 方法返回一个 Objective-C 对象,字符串上的保留计数为 +0(非拥有引用)

[sportName release];

错误: 调用者此时不拥有的对象的引用计数的不正确递减

我正在使用它来填充我的 TableView,并且在加载后崩溃:(。

I need to concatenate some strings.

NSString * sportName = [[NSString alloc ]initWithString:@""];
sportName = [sportName stringByAppendingString:[someObject getString]];
// can be more stringByAppendingString
...
[sportName release];

But something strange for me give the 'build and analyze' command:

on string:

sportName = [sportName stringByAppendingString:[someObject getString]]; 

the error:
Method returns an Objective-C object with a +0 retain count (non-owning reference)

on string:

[sportName release];

the error:
Incorrect decrement of the reference count of an object that is not owned at this point by the caller

I am using it for filling my TableView and it is crashed after loading :(.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

伏妖词 2024-11-15 13:57:41
NSString * sportName = [[NSString alloc ]initWithString:@""];
sportName = [sportName stringByAppendingString:[someObject getString]];

在第二行中,您将获得一个新字符串,该字符串被分配回 spotName。因此第一行分配的字符串被泄漏。我不明白为什么你需要附加一个空字符串。附加到 @"" 实际上没有任何效果。如果您有一些非空字符串,那么您可以将其创建为自动释放字符串而不是分配。像这样:

// create an autoreleased string
NSString * sportName = @"my_string";
// this will return a new string which is also autoreleased
sportName = [sportName stringByAppendingString:[someObject getString]];
// all subsequent call to stringByAppendingString will return autoreleased string
//so no need to release spotName

如果您需要保留 spotName 那么您可以保留它或使用属性。

NSString * sportName = [[NSString alloc ]initWithString:@""];
sportName = [sportName stringByAppendingString:[someObject getString]];

In 2nd line you are getting a new string which is assigned back to spotName. So the alloced string in first line is leaked. I don't understand why you need to append in an empty string. Appending to @"" is practically of no effect. And if you have some non-empty string then you can create that as autoreleased string instead of alloc. Like this:

// create an autoreleased string
NSString * sportName = @"my_string";
// this will return a new string which is also autoreleased
sportName = [sportName stringByAppendingString:[someObject getString]];
// all subsequent call to stringByAppendingString will return autoreleased string
//so no need to release spotName

And if you need to retain spotName then you can retain that or use a property.

つ低調成傷 2024-11-15 13:57:41

好吧,为什么你需要分配(并保留)你的字符串,如果你只是在一个方法中使用它(看起来)......

尝试这个:

NSString * sportName = @"";

而不是你的“分配”:

并删除释放线......

well, why do you need to alloc (and so retain) your string if you use it just inside a method (as it seems)...

try this:

NSString * sportName = @"";

instead of your "alloc":

and remove the release line...

烂柯人 2024-11-15 13:57:41

您首先通过发送 alloc(或 new、copy、mutableCopy、retain)来拥有 sportName。现在您已经有了一个新的实例,您必须稍后发布它。

stringByAppendingString 返回您分配给 sportName 的自动释放字符串。引用已丢失且永远无法释放。

查看内存地址:

NSString *sportName = [[NSString alloc ]initWithString:@"baa"];
NSLog(@"<%p> %@", sportName, sportName);
sportName = [sportName stringByAppendingString:@" foo"];
NSLog(@"<%p> %@", sportName, sportName);

输出:

2011-05-20 08:19:08.675 foo[1262:207] <0x3038> baa
2011-05-20 08:19:08.677 foo]1262:207] <0x4e40bd0> baa foo

sportname 已被覆盖。

您可以通过引入 tmp 或使用字符串文字来解决此问题:

NSString *sportName = @"baa";

...
NSString *tmp = [sportName stringByAppendingString:@" foo"];

You own sportName in the first place by sending alloc(or new, copy, mutableCopy, retain). Now you've got a fresh instance which you have to release later on.

stringByAppendingString returns an autoreleased string which you assign to sportName. The reference is lost and can never be released.

Look at the memory addresses:

NSString *sportName = [[NSString alloc ]initWithString:@"baa"];
NSLog(@"<%p> %@", sportName, sportName);
sportName = [sportName stringByAppendingString:@" foo"];
NSLog(@"<%p> %@", sportName, sportName);

Output:

2011-05-20 08:19:08.675 foo[1262:207] <0x3038> baa
2011-05-20 08:19:08.677 foo]1262:207] <0x4e40bd0> baa foo

sportname has been overwritten.

You can fix that by introducing a tmp or use a string literal:

NSString *sportName = @"baa";

or

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