optional-values

fatal error: unexpectedly found nil while unwrapping an Optional value(When adding to a array)

喜夏-厌秋 提交于 2020-01-05 02:30:12
问题 I have some code that receives value from a segue and replaces a certain element of an array with the index number that I have. The initialization of the variables is: var noteTitles: [String] = ["Sample Note"] var noteBodies: [String] = ["This is what lies within"] var selectedNoteIndex: Int! var newTitle: String! var newBody: String! and I have a segue that makes the last 3 values the values that I want them to be. under viewDidLoad(), I have this: if newTitle == nil && newBody == nil { }

Difference between optional values in swift?

限于喜欢 提交于 2019-12-24 03:23:07
问题 What is the difference between: var title:String? = "Title" //1 var title:String! = "Title" //2 var title:String = "Title" //3 What am I saying if I were to set title in each way and am I forced to unwrap each variable in a different way? 回答1: Think about ? and ! like a box that might have a value or not. I recommend this article. Optional box that might have value or might not , and that optional box is not unwrapped. var title:String? = "Title" //second and third picture You use unwrapped

Why does Swift 2 favor forced unwrap over optionals?

南笙酒味 提交于 2019-12-20 06:38:12
问题 I no longer see Xcode complaining that certain things need optionals (the "?"). Now it is always forced unwrapped (the bang "!"). Is there any reason to use optionals anymore when we now have forced unwrap? 回答1: I don't really know what you mean when you write that you no longer see Xcode complaining that "certain things need optionals. Now it is always forced unwrapped" . These two sentences contradict eachother: You may have non-optional variables as much as you wish, but optional can

Optionally adding items to a Scala Map

余生长醉 提交于 2019-12-03 06:40:35
问题 I am looking for an idiomatic solution to this problem. I am building a val Scala (immutable) Map and would like to optionally add one or more items: val aMap = Map(key1 -> value1, key2 -> value2, (if (condition) (key3 -> value3) else ???)) How can this be done without using a var ? What should replace the ??? ? Is it better to use the + operator? val aMap = Map(key1 -> value1, key2 -> value2) + (if (condition) (key3 -> value3) else ???)) One possible solution is: val aMap = Map(key1 ->

Why does Swift 2 favor forced unwrap over optionals?

旧时模样 提交于 2019-12-02 09:30:38
I no longer see Xcode complaining that certain things need optionals (the "?"). Now it is always forced unwrapped (the bang "!"). Is there any reason to use optionals anymore when we now have forced unwrap? I don't really know what you mean when you write that you no longer see Xcode complaining that "certain things need optionals. Now it is always forced unwrapped" . These two sentences contradict eachother: You may have non-optional variables as much as you wish, but optional can really nice once you get to know all the benefits of using them. If you have a property that is not an optional,

Why is Optional(“Text”) - Swift

大城市里の小女人 提交于 2019-11-27 07:29:45
问题 I just started with Swift. So I created a simple application with a label, button and a text field. When you click the button, the app has to change the label with the text of the text field. class ViewController: UIViewController { @IBOutlet weak var textLabel: UILabel! @IBOutlet weak var textField: UITextField! @IBAction func updateButton(sender: UIButton) { textLabel.text = "Hi \(textField.text) " } The result is Hi Optional("TextOfTextField") Okay. So it's a very simple question. I hope