字符串常量和字符串文字之间有什么区别? [英] What's the difference between a string constant and a string literal?

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

问题描述

我正在学习Objective-C和Cocoa,并且遇到过以下说法:

I'm learning objective-C and Cocoa and have come across this statement:

Cocoa框架希望将全局字符串常量而不是字符串文字用于字典键,通知和异常名称以及一些采用字符串的方法参数.

The Cocoa frameworks expect that global string constants rather than string literals are used for dictionary keys, notification and exception names, and some method parameters that take strings.

我只使用高级语言工作,因此不必过多考虑字符串的细节.字符串常量和字符串文字有什么区别?

I've only worked in higher level languages so have never had to consider the details of strings that much. What's the difference between a string constant and string literal?

推荐答案

在Objective-C中,语法@"foo"不可变,文字实例>.它不会像Mike假设的那样从字符串文字中生成常量字符串.

In Objective-C, the syntax @"foo" is an immutable, literal instance of NSString. It does not make a constant string from a string literal as Mike assume.

Objective-C编译器通常在编译单元中 do 个内部文字字符串,也就是说,它们合并了同一文字字符串的多种用法,而且链接程序有可能在编译单元之间进行额外的实习.直接链接到单个二进制文件中. (由于Cocoa区分了可变字符串和不可变字符串,并且文字字符串也始终是不可变的,所以这可以直接又安全.)

Objective-C compilers typically do intern literal strings within compilation units — that is, they coalesce multiple uses of the same literal string — and it's possible for the linker to do additional interning across the compilation units that are directly linked into a single binary. (Since Cocoa distinguishes between mutable and immutable strings, and literal strings are always also immutable, this can be straightforward and safe.)

常量字符串通常使用以下语法声明和定义:

Constant strings on the other hand are typically declared and defined using syntax like this:

// MyExample.h - declaration, other code references this
extern NSString * const MyExampleNotification;

// MyExample.m - definition, compiled for other code to reference
NSString * const MyExampleNotification = @"MyExampleNotification";

这里的语法练习的重点是,通过确保即使在多个框架中,使用的字符串只有一个实例,可以使字符串的使用高效 (共享库)在相同的地址空间中. (const关键字的位置很重要;它确保指针本身保证是恒定的.)

The point of the syntactic exercise here is that you can make uses of the string efficient by ensuring that there's only one instance of that string in use even across multiple frameworks (shared libraries) in the same address space. (The placement of the const keyword matters; it guarantees that the pointer itself is guaranteed to be constant.)

虽然在25MHz 68030工作站和8MB RAM的情况下燃烧内存并不像以前那么大,但是比较字符串是否相等可能需要一些时间.确保大多数时间上相等的字符串也将成为指针相等的帮助.

While burning memory isn't as big a deal as it may have been in the days of 25MHz 68030 workstations with 8MB of RAM, comparing strings for equality can take time. Ensuring that most of the time strings that are equal will also be pointer-equal helps.

例如,您要按名称订阅来自对象的通知.如果您使用非恒定字符串作为名称,则在确定谁对该字符串感兴趣时,发布通知的NSNotificationCenter可能会进行大量的逐字节字符串比较.如果由于被比较的字符串具有相同的指针而使大多数比较短路,那将是一个很大的胜利.

Say, for example, you want to subscribe to notifications from an object by name. If you use non-constant strings for the names, the NSNotificationCenter posting the notification could wind up doing a lot of byte-by-byte string comparisons when determining who is interested in it. If most of these comparisons are short-circuited because the strings being compared have the same pointer, that can be a big win.

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

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