我需要释放一个常量NSString吗? [英] Do I need to release a constant NSString?

查看:70
本文介绍了我需要释放一个常量NSString吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读内存管理规则,直到它说

I'm reading memory management rules to this point where it said

- (void)printHello {

    NSString *string;

    string = [[NSString alloc] initWithString:@"Hello"];

    NSLog(@"%@", string);

    [string release];

}

您拥有所有权,必须释放string,但是我对@"Hello"很好奇. @" "是用于创建和NSString的语法,它是一个对象.那不会泄漏吗?

you have ownership and have to release string, but I'm curious about the @"Hello". @" " is the syntax for creating and NSString, and it's an object. So doesn't that get leaked?

推荐答案

@"…"NSString的文字实例.当编译器看到文字字符串时,它将字符串映射到二进制文件中(例如您的程序),并且在加载二进制文件时(例如在运行程序时)该字符串可用作NSString对象.您不必管理文字字符串所占用的内存,因为它们是二进制文件的内在部分-它们始终可用,永远不会被释放,并且您不必担心管理它们的内存.

@"…" is a literal instance of NSString. When the compiler sees a literal string, it maps the string into the binary file (e.g. your program) and the string is available as an NSString object when the binary is loaded (e.g. when you run your program). You don’t have to manage the memory occupied by literal strings because they’re an intrinsic part of your binary — they are always available, they never get released, and you don’t have to worry about managing their memory.

这篇关于我需要释放一个常量NSString吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆