What's the difference between a string constant and a string literal?
I’m learning objective-C and Cocoa and have come across this statement:
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.
How to convert String to UnsafePointer<UInt8> and length Strange return value after generating random string (Swift 3) Swift 3 error: unrecognized selector sent to instance Is there a way to create a String from utf16 array in swift? <string.h> conflicting with my own String.h Find index of Nth instance of substring in string in Swift
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?
- how do i convert a hex string to its unicode ascii equivalent in swift?
- Swift 3: convert a null-terminated UnsafePointer<UInt8> to a string
- Using scanf with NSStrings
- Swift 2 : Iterating and upper/lower case some characters
- How do I convert a string into an integer in objective C?
- UIPickerView selected time to run code
3 Solutions Collect From Internet About “What's the difference between a string constant and a string literal?”
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 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";
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.)
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.
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.
A literal is a value, which is immutable by definition. eg:
A constant is a read-only variable or pointer. eg:
const int age = 10;
A string literal is a expression like
@"". The compiler will replace this with an instance of
A string constant is a read-only pointer to
NSString *const name = @"John";
Some comments on the last line:
- That’s a constant pointer, not a constant object1.
objc_sendMsg2 doesn’t care if you qualify the object with
const. If you want an immutable object, you have to code that immutability inside the object3.
@""expressions are indeed immutable. They are replaced4 at compile time with instances of
NSConstantString, which is a specialized subclass of
NSStringwith a fixed memory layout5. This also explains why
NSStringis the only object that can be initialized at compile time6.
A constant string would be
const NSString* name = @"John"; which is equivalent to
NSString const* name= @"John";. Here, both syntax and programmer intention are wrong:
const <object> is ignored, and the
NSString instance (
NSConstantString) was already immutable.
1 The keyword
const applies applies to whatever is immediately to its left. If there is nothing to its left, it applies to whatever is immediately to its right.
2 This is the function that the runtime uses to send all messages in Objective-C, and therefore what you can use to change the state of an object.
3 Example: in
const NSMutableArray *array = [NSMutableArray new]; [array removeAllObjects]; const doesn’t prevent the last statement.
4 The LLVM code that rewrites the expression is
RewriteModernObjC::RewriteObjCStringLiteral in RewriteModernObjC.cpp.
5 To see the
NSConstantString definition, cmd+click it in Xcode.
6 Creating compile time constants for other classes would be easy but it would require the compiler to use a specialized subclass. This would break compatibility with older Objective-C versions.
Back to your quote
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.
It says that literals are error prone. But it doesn’t say that they are also slower. Compare:
// string literal [dic objectForKey:@"a"]; // string constant NSString *const a = @"a"; [dic objectForKey:a];
In the second case I’m using keys with const pointers, so instead
[a isEqualToString:b], I can do
(a==b). The implementation of
isEqualToString: compares the hash and then runs the C function
strcmp, so it is slower than comparing the pointers directly. Which is why constant strings are better: they are faster to compare and less prone to errors.
If you also want your constant string to be global, do it like this:
// header extern NSString *const name; // implementation NSString *const name = @"john";
Let’s use C++, since my Objective C is totally non-existent.
If you stash a string into a constant variable:
const std::string mystring = "my string";
Now when you call methods, you use my_string, you’re using a string constant:
Or, you can call those methods with the string literal directly:
The reason, presumably, that they encourage you to use string constants is because Objective C doesn’t do “interning”; that is, when you use the same string literal in several places, it’s actually a different pointer pointing to a separate copy of the string.
For dictionary keys, this makes a huge difference, because if I can see the two pointers are pointing to the same thing, that’s much cheaper than having to do a whole string comparison to make sure the strings have equal value.
Edit: Mike, in C# strings are immutable, and literal strings with identical values all end pointing at the same string value. I imagine that’s true for other languages as well that have immutable strings. In Ruby, which has mutable strings, they offer a new data-type: symbols (“foo” vs. :foo, where the former is a mutable string, and the latter is an immutable identifier often used for Hash keys).
- Create my own completion blocks in iOS
- How to add two custom cell into tableview in storyboard in Swift?
- What happens to a .h file that is not included in a target during compilation?
- jQuery Mobile fixed footer is moving when the keyboard appears
- Add Instagram to UIActivityViewController
- the button is disabled when text view is empty else is enabled
- Convert date string swift
- Display status bar on landscape orientation for iOS 8
- Test app on iPhone without paying $99 to Apple
- Local and Global variables in Swift
- iOS builds / ipa creation no longer works from the command line
- Why do I not need to declare UIAlertViewDelegate in the header?
- Show NSData as binary in a NSString
- In iOS, how do I create a button that is always on top of all other view controllers?
- Filter array by indices