2

What I want to know is how to create in swift a string with notation on localizable file and replace this notation just before.

"welcome" = "Hello %@, Welcome!"
"seeYou"  = "Goodbye %@"
"update"  = "All your profile data was update, %@"

in another file:

func showMessage(name : String){
  print(welcome,name)
}

thanks for the help,

Filipe

2
  • let *name = "Filipe" var welcome = "Hello \ (name), Welcome!" print("result is :\ (welcome )") Commented Oct 30, 2015 at 17:50
  • 1
    this is not exactly a duplicate of the answer mentioned above. Commented Aug 16, 2016 at 6:42

2 Answers 2

7

It's much easier in Swift than in Objective-C by using String Interpolation

let name = "Filipe"
print("Hello \(name), Welcome!")

or the plus operator

print("Hello " + name + ", Welcome!")

In an environment to process localizable strings use

let welcome = "Hello %@, Welcome!"

func showMessage(name : String){
  print(String(format: NSLocalizedString(welcome, comment: ""), name))
}

showMessage("Filipe")
Sign up to request clarification or add additional context in comments.

2 Comments

printalso supports this now: print("Hello", name, ", Welcome!") String doesn't :(
Yes, but you'll get an unwanted space character after the name variable in this example.
7

You can do this:

func showMessage(name : String) {
    let msg : String = String(format: "Hello %@, Welcome!", name)
    print(msg)
}

Check this link.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.