Search code examples
objective-cstring

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

  • 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.