SRMR
SRMR

Reputation: 3134

Using Cell Tags and objectForKey in Swift

I'm converting my project from Objective-C to Swift, and trying to figure how to write this in Swift:

UILabel *tvLabel = (UILabel*) [cell viewWithTag:104];
tvLabel.text = [object objectForKey:@"tv"];

I've tried a few things different things like this:

    if let tvLabel = object?["tv"] as? String {
        cell?.textLabel?.text = cell.viewWithTag(4) as? UILabel
    }

... but they all have errors and I can't seem to get the solution.

Any ideas?

Upvotes: 0

Views: 69

Answers (1)

donnywals
donnywals

Reputation: 7591

You probably want something like:

if let tvLabel = cell?.viewWithTag(4) as? UILabel {
    tvLabel.text = object.objectForKey("tv") as! String
}

Safer way:

if let tvLabel = cell?.viewWithTag(4) as? UILabel, let tvString = object?.objectForKey("tv") as? String {
    tvLabel.text = tvString
}

Upvotes: 1

Related Questions