如何在Swift中创建动态字符串?

Fil*_*ria 2 string ios swift

我想知道的是如何在swift中创建一个带有可本地化文件的符号的字符串,并在之前替换这种表示法.

"welcome" = "Hello %@, Welcome!"
"seeYou"  = "Goodbye %@"
"update"  = "All your profile data was update, %@"
Run Code Online (Sandbox Code Playgroud)

在另一个文件中:

func showMessage(name : String){
  print(welcome,name)
}
Run Code Online (Sandbox Code Playgroud)

谢谢您的帮助,

菲利佩

vad*_*ian 6

通过使用字符串插值,在Swift中比在Objective-C中容易得多

let name = "Filipe"
print("Hello \(name), Welcome!")
Run Code Online (Sandbox Code Playgroud)

或加号运算符

print("Hello " + name + ", Welcome!")
Run Code Online (Sandbox Code Playgroud)

在一个处理可本地化字符串的环境中使用

let welcome = "Hello %@, Welcome!"

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

showMessage("Filipe")
Run Code Online (Sandbox Code Playgroud)


hug*_*rdo 5

你可以这样做:

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

检查此链接.