1

I have an Xcode project with a label and a UIAction button. Upon tapping the button, I want the value displayed in the label to increase by 1. The problem is that I can't display the variable "value" in the label, because it's an integer, and the label takes a String data type. Here is the code I have so far:

var value = 0 

@IBOutlet var label: UILabel!

@IBAction func button(sender: AnyObject) {
    value = value + 1
    label.text = value
}
2
  • value.description or string interpolation "\(value)" Commented Mar 25, 2015 at 3:28
  • possible duplicate of Convert Int to String in Swift Commented Mar 25, 2015 at 3:30

3 Answers 3

17
label.text = String(value)

Or

label.text = "\(value)"

Or

label.text = value.description
Sign up to request clarification or add additional context in comments.

3 Comments

Which do you use most? .description or String()
In Swift 3 ` String(describing: value)` is preferred
@LinhChu String(describing: ) may return "Optional(intValue)" as the string while .description does not
0

Of course I would save some typing and express it this way:

value++ 
label.text = value.description

In the end it is the same thing as:

value = value + 1
label.text = value.description

Comments

-1

you can convert value before saving core data directly just like that

//core data 
userid value = string

we need to store Int value we have to convert the value explicitly like that. I tried that and it works fine core data contain task entity having user_id attribute

 tasksEntity.user_id = string

that should work

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.