ansariha
ansariha

Reputation: 61

How do I re-write this Objective-C line into Swift?

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

I'm trying to re-write the above line of code into Swift, as it's currently written in Objective-C. For the sake of context, I'm trying to retrieve a value in my Parse database under the column header "VoteCount", which happens to be the class name. Here's the variable that includes all the columns:

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

Upvotes: 0

Views: 93

Answers (2)

Nate Cook
Nate Cook

Reputation: 93276

The Swift String type has an initializer just like the [NSString stringWithFormat:] factory method:

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

Upvotes: 0

Michael Dautermann
Michael Dautermann

Reputation: 89509

updated answer:

taking Thilo's comment into account... how about:

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)"

original answer:

I think it should be something like:

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

Upvotes: 1

Related Questions