NSString 文字之间的区别
这两行有什么区别?
NSString * string = @"My String";
NSString * string = [[[NSString alloc] initWithString:@"MyString"] autorelease]
What is the difference between these two lines?
NSString * string = @"My String";
NSString * string = [[[NSString alloc] initWithString:@"MyString"] autorelease]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
只要记住这个基本的事情:-
这是一个指向对象的指针,“不是对象”!
因此,语句:
NSString *string = @"Hello";
将@"Hello"
对象的地址分配给指针字符串。@"Hello"
被编译器解释为常量字符串,编译器自己为其分配内存。类似地,该语句
将 SomethingElse 的地址分配给指针
myObject
,并且该somethingElse
应该已经被分配和初始化。因此,语句:
NSObject *myObject = [[NSObject alloc] init];
分配并初始化一个NSObject
对象,并将其地址分配给myObject
。Just remember this basic thing:-
This is a pointer to an object, "not an object"!
Therefore, the statement:
NSString *string = @"Hello";
assigns the address of@"Hello"
object to the pointer string.@"Hello"
is interpreted as a constant string by the compiler and the compiler itself allocates the memory for it.Similarly, the statment
assigns the address of somethingElse to pointer
myObject
, and thatsomethingElse
should already be allocated ad initialised.Therefore, the statement:
NSObject *myObject = [[NSObject alloc] init];
allocates and initializes aNSObject
object and assigns its address tomyObject
.@“My String”是编译为二进制文件的文字字符串。加载后,它在内存中占有一席之地。第一行声明一个指向内存中该点的变量。
来自字符串编程指南:
第二行通过获取该文字字符串来分配一个字符串。请注意,两个@“My String”文字字符串是相同的。为了证明这一点:
输出相同的内存地址:
这不仅说明前两个字符串具有相同的内存地址,而且如果不更改代码,每次运行它时它都是相同的内存地址。它与内存中的二进制偏移量相同。但是,不仅副本不同,而且每次运行它时都会不同,因为它是在堆上分配的。
根据上面的文档参考,自动释放没有影响。您可以释放它们,但它们永远不会被释放。因此,它们相等并不是因为两者都是自动释放的字符串,而是因为它们都是常量并且释放被忽略。
@"My String" is a literal string compiled into the binary. When loaded, it has a place in memory. The first line declares a variable that points to that point in memory.
From the string programming guide:
The second line allocates a string by taking that literal string. Note that both @"My String" literal strings are the same. To prove this:
Outputs the same memory address:
What's telling is not only are the first two string the same memory address, but if you don't change the code, it's the same memory address every time you run it. It's the same binary offset in memory. But, not only is the copy different but it's different every time you run it since it's allocated on the heap.
The autorelease has no affect according to the doc ref above. You can release them but they are never deallocated. So, they are equal not because both are autoreleased string but that they're both constants and the release is ignored.
一种是文字字符串,它在执行应用程序的生命周期中持续存在。另一个可能是一个动态对象,仅持续到自动释放为止。 (如果系统决定以这种方式优化它,它也可能是一个文字字符串——不能保证它不会。)
One is a literal string, which persists for the life of the executing app. The other may be a dynamic object that only persists until autoreleased. (It may also be a literal string, if the system decides to optimize it that way -- there are no guarantees it won't.)
bryanmac 的回答 100% 正确。我刚刚使用 GHUnit 添加了一个明确的示例。
NSString
创建 - 文字与非文字。显示以各种方式创建的字符串(无论是文字还是非文字)。
bryanmac is 100% correct in his answer. I just added an explicit example using GHUnit.
NSString
creation - literal vs nonliteral.Shows strings created in various ways if they are literal nor nonliteral.
它们之间没有区别。您在第一个示例中显示的启动方式的字符串是自动释放的字符串。
There is no difference between them. A string initiated how you showed in the first example is an autoreleased string.