NSString 文字之间的区别 [英] Difference between NSString literals

查看:19
本文介绍了NSString 文字之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这两行有什么区别?

NSString * string = @"My String";NSString * string = [[[NSString alloc] initWithString:@"MyString"] autorelease]

解决方案

@"My String" 是编译成二进制的文字字符串.加载后,它在内存中占有一席之地.第一行声明了一个指向内存中那个点的变量.

来自字符串编程指南:

<块引用>

在源代码中创建字符串对象最简单的方法是使用Objective-C @"..." 构造:

NSString *temp = @"/tmp/scratch";

<块引用>

注意,创建字符串时以这种方式保持不变,您应该避免使用 7 位以外的任何东西ASCII 字符.这样的对象在编译时创建并存在在程序的整个执行过程中.编译器生成这样的对象每个模块的常量都是唯一的,并且它们永远不会被释放,尽管您可以像处理任何其他对象一样保留和释放它们.

第二行通过采用该文字字符串来分配一个字符串.请注意,两个 @"My String" 文字字符串是相同的.为了证明这一点:

NSString *str = @"My String";NSLog(@"%@(%p)", str, str);NSString *str2 = [[NSString alloc] initWithString:@"My String"];NSLog(@"%@(%p)", str2, str2);NSString *copy = [str2 stringByAppendingString:@"2"];NSLog(@"%@(%p)", copy, copy);

输出相同的内存地址:

2011-11-07 07:11:26.172 Craplet[5433:707] 我的字符串 (0x100002268)2011-11-07 07:11:26.174 Craplet [5433:707] 我的字符串 (0x100002268)2011-11-07 07:11:26.174 Craplet [5433:707] 我的 String2 (0x1003002a0)

说明不仅前两个字符串具有相同的内存地址,而且如果您不更改代码,则每次运行它时都是相同的内存地址.它与内存中的二进制偏移量相同.但是,不仅副本不同,而且每次运行它都不一样,因为它是在堆上分配的.

根据上面的文档参考,自动释放没有影响.您可以释放它们,但它们永远不会被释放.所以,它们相等并不是因为它们都是自动释放的字符串,而是因为它们都是常量并且释放被忽略.

What is the difference between these two lines?

NSString * string = @"My String";
NSString * string = [[[NSString alloc] initWithString:@"MyString"] autorelease]

解决方案

@"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 simplest way to create a string object in source code is to use the Objective-C @"..." construct:

NSString *temp = @"/tmp/scratch"; 

Note that, when creating a string constant in this fashion, you should avoid using anything but 7-bit ASCII characters. Such an object is created at compile time and exists throughout your program’s execution. The compiler makes such object constants unique on a per-module basis, and they’re never deallocated, though you can retain and release them as you do any other object.

The second line allocates a string by taking that literal string. Note that both @"My String" literal strings are the same. To prove this:

NSString *str = @"My String";
NSLog(@"%@ (%p)", str, str);

NSString *str2 = [[NSString alloc] initWithString:@"My String"];
NSLog(@"%@ (%p)", str2, str2);

NSString *copy = [str2 stringByAppendingString:@"2"];
NSLog(@"%@ (%p)", copy, copy);

Outputs the same memory address:

2011-11-07 07:11:26.172 Craplet[5433:707] My String (0x100002268)
2011-11-07 07:11:26.174 Craplet[5433:707] My String (0x100002268)
2011-11-07 07:11:26.174 Craplet[5433:707] My String2 (0x1003002a0)

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.

这篇关于NSString 文字之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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