NSString 基础知识澄清

发布于 2024-12-23 08:19:03 字数 648 浏览 3 评论 0原文

可能的重复:
NSString 保留计数

我是iPhone编程的初学者。我正在处理 NSString。我有一个疑问,解释如下。

@implementation Sample;

NSString *str;

-(void)viewDidLoad
{
    str = [[NSString alloc] initWithString:@"Hello"];

    // Why retain count is some random value? (Eg.2147234)
    NSLog(@"retain count of string %d",[str retainCount]);

    [str release];
}

-(void)printString
{
    // Why the value for "str" getting printed here,
    // though its released in viewDidLoad?
    NSLog(@"string is %@",str);
}

Possible Duplicate:
NSString retain Count

I am the beginner for iPhone programming. I am dealing with NSString. I got a doubt explained below.

@implementation Sample;

NSString *str;

-(void)viewDidLoad
{
    str = [[NSString alloc] initWithString:@"Hello"];

    // Why retain count is some random value? (Eg.2147234)
    NSLog(@"retain count of string %d",[str retainCount]);

    [str release];
}

-(void)printString
{
    // Why the value for "str" getting printed here,
    // though its released in viewDidLoad?
    NSLog(@"string is %@",str);
}

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

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

发布评论

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

评论(2

救星 2024-12-30 08:19:03
  1. 不要看retainCount。它会让你感到困惑,而且没有帮助。

  2. 常量字符串直接构建到代码中——它们永远不会被分配或释放。您可以像对待任何其他对象一样保留和释放它们,但不要指望常量字符串在任何时候都会被释放。

  1. Don't look at retainCount. It'll confuse you, and it doesn't help.

  2. Constant strings are built right into the code -- they're never allocated or released. It's fine for you to retain and release them as you would any other object, but don't expect a constant string to be deallocated at any point.

〗斷ホ乔殘χμё〖 2024-12-30 08:19:03

在 Objective-C 中,init 方法不一定返回使用 alloc 创建的相同对象。它可能会释放 self,然后返回另一个对象。

在 initWithString 的情况下,它很有可能返回 @"Hello" 常量字符串对象,而不是初始化一个新字符串,因为它更快并且没有负面影响(两个字符串都是不可变的)。

正如@Caleb 所说,正常的内存管理规则不适用于常量字符串。你无法释放它,它永远都在那里。

但所有这些都是无证行为,并且可能会发生变化。你不能依赖它,并且你发布的代码有缺陷的,在发布了一些你不应该尝试访问它的东西之后。

相反,您应该遵循标准实践,即您应该始终释放一个对象,并在使用完该对象后将指向该对象的所有指针设置为 nil。如果您在释放后将 str 设置为 nil,您将看到预期的行为。

或者更好的是,只需打开 ARC,然后忘记所有这些事情。

In objective-c, an init method doesn't necessarily return the same object created using alloc. It may release self and then return another object.

In the case of initWithString there's a good chance it returns the @"Hello" constant string object, instead of initialising a new string, since it's faster and has no negative side effects (both strings are immutable).

As @Caleb said, normal memory management rules don't apply to constant strings. You can't release it, it will always be there.

But all of this is undocumented behaviour and subject to change. You can't depend on it, and the code you've posted is buggy, after releasing something you shouldn't ever try to access it.

Instead you should follow standard practices, which says you should always release an object and set any pointers to it to nil when you're done with it. If you had set str to nil after releasing it, you would be seeing the expected behaviour.

Or even better, just turn ARC on, and forget about all these things.

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