如何将此Objective-C行重写为Swift?

时间:2014-11-06 00:28:43

标签: objective-c swift

yourLabelName.text = [[NSString] stringWithFormat:@"The number of votes for Crepes is %d", voteCount1["votes"]];

我试图将上面的代码重写到Swift中,因为它目前用Objective-C编写。为了上下文,我尝试在列标题" VoteCount"下检索我的Parse数据库中的值,这恰好是类名。这是包含所有列的变量:

    var voteCount1 = PFObject(className: "VoteCount")
    voteCount1["votes"] = 0
    voteCount1["optionName"] = "crepes"

2 个答案:

答案 0 :(得分:1)

更新回答:

考虑到Thilo的评论......怎么样:

var voteCount1 = PFObject(className: "VoteCount")
let optionName = voteCount1["optionName"];
let votes      = voteCount1["votes"] as String;

yourNameLabel.text = "The number of votes for \(optionName) is \(votes)"

原始答案:

我认为应该是这样的:

var voteCount1 = PFObject(className: "VoteCount")
yourNameLabel.text = 
   "The number of votes for \(voteCount1["optionName"]) is \(voteCount1["votes"])"

答案 1 :(得分:0)

Swift String类型有一个初始值设定项,就像[NSString stringWithFormat:]工厂方法一样:

yourLabelName.text = String(format: "The number of votes for Crepes is %d", voteCount1["votes"])