xxtmanxx
xxtmanxx

Reputation: 209

How do I convert a UILabel to a string?

I am trying to cast a UILabel to a string, but it always fails. How else would I cast it? friendname is a UILabel, and friendusername is a string.

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if (segue.identifier == "friendsSongs"){

        let nextViewOBJ = segue.destinationViewController as! FriendsMusic
        nextViewOBJ.friendusername = friendname as! String
    }
}

Upvotes: 0

Views: 3523

Answers (2)

JAL
JAL

Reputation: 42449

You don't want to convert a UILabel to a string, you want to access its text property:

nextViewOBJ.friendusername = friendname.text

Upvotes: 4

Undo
Undo

Reputation: 25687

Use the text attribute on UILabel:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if (segue.identifier == "friendsSongs"){

        let nextViewOBJ = segue.destinationViewController as! FriendsMusic
        nextViewOBJ.friendusername = friendname.text
    }
}

You can't cast a UILabel to a String - but you can find what the text of a UILabel is. That's what you want to do here.

Upvotes: 7

Related Questions