3

Now only i am start working with swift language. I have initialize the button as follows

@IBOutlet weak var systemTextButton: UIButton!

after that i can set the title using .setTitle() property.

If i use ? instead of ! while initialize then showing the error on .setTitle() as

UIButton? does not have a member named 'setTitle'

So, Anyone please tell me the difference between UIButton! and UIButton? .

Surfer
  • 1,370
  • 3
  • 19
  • 34

6 Answers6

4

Both of them are optional types. But UIButton! is implicitly unwrapping optional type. You don't have to unwrap it manually. Take a not that your app will crash if you try to access value of UIButton! while its nil.

For more info read "Implicitly Unwrapped Optionals" section in Apple's "The Swift Programming Language" book.

Kirsteins
  • 27,065
  • 8
  • 76
  • 78
2

The button is optional because it is not determined from the beginning if the button will be found in the interface builder document. So either

  • you are sure it will be there, in which case you unwrap it right in the declaration,
  • or it is really an optional - then you have to unwrap it each time you want to use it.

Maybe the existence of the button in interface builder is somehow dynamic, then you would keep it optional (UIButton?) to allow a nil value. In this case you have to either check for its existence before you use it, or unwrap it with myButton!. You do this at your own risk because if you try it on a nil value your program will crash.

Mundi
  • 79,884
  • 17
  • 117
  • 140
2

Both variants are dealing with optionals (variables that may be nil).

@IBOutlet weak var systemTextButton: UIButton!

This is an implicitly unwrapped optional. The exclamation mark states that you (the programmer) are absolutely sure that this variable is not nil when using. You set the title via

systemTextButton.setTitle(...)

If systemTextButton is nil at that point the application crashes.

The questionmark states that this variable may be nil, so you have to use:

systemTextButton?.setTitle(...)

Now, setTitle() is only executed if systemTextButton is not nil.

Hope that helps.

For further reference on optionals see The Swift Programming Language

zisoft
  • 22,770
  • 10
  • 62
  • 73
1

In this two examples the UIButton is declared as optional. The difference is that when you declare it as UIButton! it's unwrapped for you automatically, you don't have to check is it nil, you should be sure that the button will not be nil.

The other declaration UIButton? doesn't unwrap this optional for you, you have to do it manually when you want to access it (or access it's properties). You can do it in two way. If you are sure that the object is not nil you can use this:

button!.setTitle()

but if you are not sure about state of this optional, use this:

if let btn = button {
     btn.setTitle()
}
else {
    // button is nil
}
Greg
  • 25,317
  • 6
  • 53
  • 62
  • If it is an optional the `if let` pattern is not necessary: `if button != nil` is enough. – Mundi Oct 10 '14 at 10:54
1

When you declare the variable of type UIButton?, you are saying to compiler that that variable could be nil (NULL in Objective-C world). So, it becomes an "optional", and all subsequent actions an that object are skipped if it's nil.

For example:

class MyClass {
    var property: String?
}

let myInstance = MyClass()
if let testVar = myInstance.property {
    println("Hello world")
} else {
    println("Property unset")
}

You can find more info in the official swift documentation:

https://developer.apple.com/librarY/prerelease/mac/documentation/Swift/Conceptual/Swift_Programming_Language/OptionalChaining.html

1

These kinds of optionals are defined as implicitly unwrapped optionals. You write an implicitly unwrapped optional by placing an exclamation mark (String!) rather than a question mark (String?) after the type that you want to make optional.

    let possibleString: String? = "An optional string."
    let forcedString: String = possibleString! // requires an exclamation mark

    let assumedString: String! = "An implicitly unwrapped optional string."
    let implicitString: String = assumedString // no need for an exclamation mark

Straight from Apple Developer Library

Kampai
  • 22,848
  • 21
  • 95
  • 95