Reputation: 338
how can i pass the label inside the button to the next activity,
here is my code but it doesn't work
@IBOutlet weak var elecB: UIButton!
my prepareForSegue code
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
var result:searchResultView = segue.destinationViewController as! searchResultView
if elecB.selected{
result.cat = elecB.titleLabel?.text!
}
}
it tells me there is an error in this line of code
result.cat = elecB.titleLabel?.text!
cat is a string
can any one help me, all i want to do is pass the label inside the button clicked to the next view controller
Upvotes: 0
Views: 106
Reputation: 153
Check out the Swift documentation for the definition of optional variables (the ones with the ? at the end of the declaration.
If you define a variable like this var cat:String?
then the variable is optional. That essentially means that the variable can have nil as a value.
From Apple's documentation: You use optionals in situations where a value may be absent. An optional says: - There is a value, and it equals x or - There isn’t a value at all
The example below uses the toInt() method to try to convert a String into an Int:
let possibleNumber = "123"
let convertedNumber = possibleNumber.toInt()
// convertedNumber is inferred to be of type "Int?", or "optional Int"
Because the toInt() method might fail, it returns an optional Int, rather than an Int. An optional Int is written as Int?, not Int. The question mark indicates that the value it contains is optional, meaning that it might contain some Int value, or it might contain no value at all. (It can’t contain anything else, such as a Bool value or a String value. It’s either an Int, or it’s nothing at all.)
Upvotes: 0
Reputation: 9178
Change the declaration of titleLabel
to titleLabel: UILabel!
or titleLabel: UILabel
, since you don't actually need it to be optional. It sounds like you'd reasonably expect for that label to exist all the time.
Making cat
optional is also not the best solution, since you expect that string to always have a value. So leave cat
as is, and make your title label non-optional, or implicitly unwrapped (!
) if Xcode complains.
?
and !
mean?A ?
after a declaration means that that variable is optional, and can be reasonably excepted to be nil
(this also allows you to set it to nil
). You also have to follow the variable name with a ?
every time you want to use it, or wrap it in an if let a = myOptionalVar {...}
, so that code will only execute if the variable is not nil
.
A !
after a declaration makes it an implicitly-unwrapped optional. This means that the variable can be nil
, but is assumed to not be nil
in most circumstances, so you don't have to follow it with an !
every time you want to use it. Note however, if the variable does happen to be nil when you try to use it, your program will crash.
!
should only be used when you absolutely have to, because it is unsafe by nature. For example, if Interface Builder requires @IBOutlet
variables to be optional, it is best to make them !
, because they will be automatically initialised when your class is created from the storyboard, and will have a value for the rest of the time.
Using neither of these means that the variable is not optional, i.e. it must always have a value, and cannot be nil
.
Upvotes: 2