What's the difference between a string constant and a string literal?
我正在学习Objective-C和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.
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. You
should always prefer string constants over string literals when you
have a choice. By using string constants, you enlist the help of the
compiler to check your spelling and thus avoid runtime errors.
它说文字容易出错。但这并不表示它们也较慢。相比:
1 2 3 4 5 6
// string literal [dic objectForKey:@"a"];
// string constant
NSString *const a = @"a"; [dic objectForKey:a];