Reputation:
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?
Upvotes: 2
Views: 839
Reputation: 1484
My tip would be defining a scope-wide macro:
#define STR(...) [NSString stringWithFormat:__VA_ARGS__]
and then use the defined STR(...)
in code like:
STR(@"%02zd/%02zd/%04zd %@:00:00", end.day, end.month, end.year, endTimeString);
to have at least a slightly less verbose variant to compose strings. 👍
Upvotes: 0
Reputation: 1
@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!
Upvotes: 0
Reputation: 318824
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.
Upvotes: 6