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

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

问题描述

我正在学习 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" 是一个 immutableliteral NSString 的实例.它不会像 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 编译器通常在编译单元内实习文字字符串——也就是说,它们合并同一个文字字符串的多次使用——并且链接器有可能在编译单元之间进行额外的实习直接链接到单个二进制文件中.(因为 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天全站免登陆