所以在Obj-c中这会有不同的做法,但我想知道是否有人知道如何在swift中做到这一点?我不得不问这个让我感到尴尬,但是根本找不到任何文档,如何将float转换为字符串?
override func viewDidLoad() {
super.viewDidLoad()
var userScore = receivePlayerCard?.playerScore
var convertUserScoreToString: Float
scoreNameLabel.text = convertUserScoreToString //need to convert it to a string here
scoreNameLabel.textColor = UIColor.whiteColor()
// Do any additional setup after loading the view.
}
答案 0 :(得分:7)
答案 1 :(得分:3)
我们可以使用格式字符串和Swift的String
类型,这要归功于Obj-C桥接!
例如:
override func viewDidLoad() {
super.viewDidLoad()
let userScore: Double = receivePlayerCard?.playerScore
scoreNameLabel.text = String(format: "%.2f", userScore)
// Do any additional setup after loading the view.
}