user2924482
user2924482

Reputation: 9140

Avoiding implicit unwrap variable

I have a question for you guys. I'm trying to avoid do implicit unwrap variables but I haven't figure out how to do it. Here is my example with implicit unwrap

class MyView : UIView {
    @IBOutlet var button : UIButton!
    var buttonOriginalWidth : CGFloat!

}

There is a way of avoid the implicit unwrap ?

I'll really appreciate your help.

Upvotes: 0

Views: 341

Answers (3)

amgalan-b
amgalan-b

Reputation: 76

Implicitly unwrapped optionals can be avoided by:

  1. Declare a initial value:

    var buttonOriginalWidth: CGFloat = 20.0
    
  2. Declare its value within init:

    var buttonOriginalWidth: CGFloat
    
    init() {
        self.buttonOriginalWidth = 20
        super.init()
    }
    
  3. You can always declare it as an optional:

    var buttonOriginalWidth: CGFloat?
    

    But that of course means you have to unwrap it yourself next time you call it.

  4. You can also use lazy to declare a property that is set after init.

That being said, you should not always avoid using implicitly unwrapped optionals. They are especially useful if you are using Storyboard. @IBOutletand @IBAction are declared mostly as implicitly unwrapped optionals.

@IBOutlet var button: UIButton!

Because if you have configured your Storyboard correctly, it will never be nil.

Upvotes: 1

vadian
vadian

Reputation: 285260

  • In case of the outlet, don't avoid it

    @IBOutlet var button : UIButton!
    

    is intended. It reveals a design error (outlet not connected) if the code crashes.

  • In case of a regular variable – especially scalar types and strings – declare it as non-optional

    var buttonOriginalWidth : CGFloat = 0.0
    

Basically use implicit unwrapped optionals only for outlets and variables which cannot be initialized via init but are supposed to have a value when they are used the first time. It's up to you – the developer – to handle the cases carefully. In all other cases if a variable needs to be optional use a regular optional (?).

Upvotes: 0

AaoIi
AaoIi

Reputation: 8396

You can avoid that by adding ? called optional instead of ! which is force unwrapping the variable for example:

@IBOutlet var button : UIButton?

Is a valid point to add it for Outlets connected from storyboard and it actually doesn't prevent you from doing that, but its by default optionals for Outlets connected from storyboard. And also for a variable:

 var buttonOriginalWidth : CGFloat?

And to use the a variable which is optional you'll need to check first that its not actually still nil using if let or guard let as the following:

if let widthIsValid = buttonOriginalWidth {
   // use the widthIsValid here
}

And if you tried to use the variable without force unwrapping the app at least will not crash for example to do like Which will print it like Optional(478431):

print(buttonOriginalWidth)

I recommend you to read about Optional Chaining in Swift.

Upvotes: 0

Related Questions