0

If you have a UITextField and you know you will constantly reference its value and you will use the value as a Double which of the following two samples make more sense? Or better yet, does any of the two make sense? Is this a good practice to follow?

Computed Property

var fieldValue:Double {
    get{
        return Double(someField.text! ?? "0") as! Double
    }   
}

Store Property as Closure

var fieldValue:Double = {
    return Double(someField.text! ?? "0") as! Double
}()
fs_tigre
  • 10,650
  • 13
  • 73
  • 146
  • 1
    Why are you force casting a `Double` into a `Double`? Also, why are you providing a default value to a force unwrapped value (which isn't an optional anymore)? – Dávid Pásztor Nov 19 '19 at 18:40
  • Just to clarify, is this how you would get the value as a `Double`? `var fieldValue: Double { Double(someField.text!) ?? 0 }` As suggested by Mojtaba Hosseini. – fs_tigre Nov 19 '19 at 19:03

1 Answers1

1

The second option is NOT going to update constantly. Since it's not stored as a closure! It stored using a closure just once.

So in this question, you have only the first option as a choice.

Some helpful notes:

  1. Force unwrapping and using nil coalescing operator at same time is nonesence:
Double(someField.text! ?? "0") // exclamation mark is nonesense here
  1. In get-only computed properties, you can get rid of the get keyword:
var fieldValue: Double {
    return Double(someField.text ?? "0") as! Double
}
  1. You don't need to forcecast a Double to Double:
var fieldValue: Double {
    return Double(someField.text ?? "0")!
}
  1. Since Swift 5.1, you don't need return keyword for single line expressions:
var fieldValue: Double { Double(someField.text ?? "0")! }
  1. Seems like you need a 0 as a default value if initializer failed to initialize a Double from the text, right?! So replace the force unwrap with a default value as well:
var fieldValue: Double { Double(someField.text ?? "0") ?? 0 }
  1. As @Dávid Pásztor mentioned and referenced, It is safe to force unwrap UITextField().text, so someField.text ?? "0" is just as useless as someField.text! ?? "0", since UITextField.text is guaranteed to be non-nil, it is only Optional due to Obj-C legacy support. So it could if you are not referencing an Obj-C, you can use it like:
var fieldValue: Double { Double(someField.text!) ?? 0 }

Other options:

You can use valueChanged event of the textField to get notified and react in a function if you want:

// Somewhere soon like `viewDidLoad`
    someField.addTarget(self, action: #selector(someFieldChanged(_:)), for: .valueChanged)
    }
    
    @objc private func someFieldChanged(_ sender: UITextField) {
        let fieldValue = Double(sender.text ?? "0") ?? 0
        ,,,
    }

Also you can use delegate methods for this. It's up to you.

Community
  • 1
  • 1
Mojtaba Hosseini
  • 95,414
  • 31
  • 268
  • 278
  • It is safe to force unwrap `UITextField.text`, so `someField.text ?? "0"` is just as useless as `someField.text! ?? "0"`, since `UITextField.text` is guaranteed to be non-nil, it is only Optional due to Obj-C legacy support. Have a look at [Why is UITextField.text Optional](https://stackoverflow.com/questions/42861115/why-is-uitextfield-text-an-optional) – Dávid Pásztor Nov 19 '19 at 18:44
  • @ Mojtaba Hosseini - Thanks a lot for the good suggestions! I guess you meant `exclamation point` not question mark. – fs_tigre Nov 19 '19 at 18:45
  • @DávidPásztor yes it's useless in pure Swift code but makes sense sometimes. Thanks for mentioning that. I added that to my answer respecting your name. – Mojtaba Hosseini Nov 19 '19 at 18:49