Can't Unwrap 'Optional.None'

Can't unwrap Optional.None error in Swift

You must use newValue in willSet block, and oldValue in didSet block

example:

class foo {
var row: String? {
willSet {
println("will set value:")
println(newValue)
}

didSet {
println("did change value:")
println(oldValue)
}
}
}

var bar = foo()

println("First time setter called")
bar.row = "First value"
println("Second time setter called")
bar.row = "Second value"

output:

First time setter called
will set value:
First value
did change value:
nil
Second time setter called
will set value:
Second value
did change value:
First value

fatal error: Can't unwrap Optional.None When using Swift with Parse

This Parse example was written in June when Swift was in Beta. Swift has continued to evolve, so some of the things they are doing are no longer legal in Swift.

Firstly, you can no longer check if an optional variable error is nil with if !error. Instead you must explicitly check against nil like this: if error == nil.

Secondly, success is declared as an implicitly unwrapped optional. It is no longer valid to check if it is true with if success. You could do if success == true but this will crash if success is nil. Instead, you could do if (success ?? false). This uses the nil coalescing operator to safely unwrap the optional Bool if it has a value or it uses false if the Bool is nil. Either way, the if will only succeed if the Bool is true.

UITextField - Can't unwrap Optional.None

As what rdelmar suggested, to solve the error, connect display to the UITextField in StoryBoard:

Sample Image

Can't UnWrap Optional - None Swift

DVC.detailedRecipeLabel is nil

Probably it's a IBOutlet that hasn't been loaded yet.

You should add a custom property to your DetailedViewController that will accept your text, save it and then you can assign it to the label in viewDidLoad.

Example:

class DetailedViewController ... {
...
var recipeName: NSString? = nil

...

override func viewDidLoad() -> () {
...
self.detailedRecipeLabel.text = self.recipeName
}
}


DVC.recipeName = segueRecipeName

Can't unwrap Optional.None. i can't figure out how or what

Your dict.valueForKey("menuPunkt") returns an optional NSMutableArray, which you're unwrapping with the exclamation point at the end of that line. If you unwrap it more gracefully you can handle the condition where dict doesn't contain a value for "menuPunkt":

if let textArray = dict.valueForKey("menuPunkt") as? NSMutableArray {
self.txt = textArray
} else {
// give self.text some sort of default value
}


Related Topics



Leave a reply



Submit