Reputation: 302
Here is my code. If defaults did not ever save the lat, lng, then give it a lat lng.
The problem is error:
fatal error: unexpectedly found nil while unwrapping an Optional value
when using self.lat
and self.lng
after the code below. Every time console print out is
the saved gps is Optional("nil")
What's the meaning for Optional("nil")
and the difference from Optional(nil)
?
I am using Xcode 7.1 and Swift 2.
var lat : String = ""
var lng : String = ""
let defaults = NSUserDefaults.standardUserDefaults()
if defaults.stringForKey("lat") != nil {
self.lat = defaults.stringForKey("lat")!
self.lng = defaults.stringForKey("lng")!
print ("the saved gps is \(defaults.stringForKey("lat"))")
} else {
self.lat = "34.7009333"
self.lng = "135.4942047"
print ("the init gps is \(self.lat) , \(self.lng)")
}
Upvotes: 0
Views: 971
Reputation: 1704
it show the actual value...
let digit = sender.currentTitle!
suppose your sender value is 10 then output is 10.
suppose your sender value is nil then app carsh.
it show the optional value
let digit = sender.currentTitle
suppose your sender value is 10 then output is optional("10").
suppose your sender value is nil then app is not carsh.
& display value like this optional("nil")
Upvotes: 0