'Objective-C equivalent of Swift "\(variable)"
The question title says it all, really. In swift you use "\()" for string interpolation of a variable. How does one do it with Objective-C?
Solution 1:[1]
There is no direct equivalent. The closest you will get is using a string format.
NSString *text = @"Tomiris";
NSString *someString = [NSString stringWithFormat:@"My name is %@", text];
Swift supports this as well:
let text = "Tomiris"
let someString = String(format: "My name is %@", text)
Of course when you use a format string like this (in either language), the biggest issue is that you need to use the correct format specifier for each type of variable. Use %@ for object pointers. Use %d for integer types, etc. It's all documented.
Solution 2:[2]
@rmaddy's Answer is the gist of it. I just wanted to follow up on his comment that "It's all documented". Well, these symbols like %@ and %d are called String Format Specifiers the documentation can be found at the following links.
Formatting String Objects https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/Strings/Articles/FormatStrings.html
String Format Specifiers https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/Strings/Articles/formatSpecifiers.html#//apple_ref/doc/uid/TP40004265-SW1
Just telling us noobs "It's all documented" isn't very helpful because often (if you're like me) you googled to find this stackoverflow post at the top of the SEO. And taking the link in hopes of finding the original documentation!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | rmaddy |
| Solution 2 | Logan Mahoney |
