I have this code part:
let strValue = String()
textfield.stringValue = strValue!
The problem is that strValue
can be nil.
For this I check it like this:
if strValues.isEmpty() {
textfield.stringValue = ""
} else {
textfield.stringValue = strValue!
}
But I there an quicker and easier way to do this?
I read something like ??
to solve it. But I don't know how to use it?
UPDATE thanks a lot for the many feedbacks. now i unterstand the ?? operator, but how i realize it in this situation?
let person = PeoplePicker.selectedRecords as! [ABPerson]
let address = person[0].value(forProperty: kABAddressProperty) as?
ABMultiValue
txtStreet.stringValue = (((address?.value(at: 0) as! NSMutableDictionary).value(forKey: kABAddressStreetKey) as! String))
how can i usee the ?? operator in the last line of my code?
UPDATE 2 Okay i got it!
txtStreet.stringValue = (((adresse?.value(at: 0) as? NSMutableDictionary)?.value(forKey: kABAddressStreetKey) as? String)) ?? ""
In Swift, nil means the absence of a value. Sending a message to nil results in a fatal error. An optional encapsulates this concept. An optional either has a value or it doesn't.
You can give your own parameters a default value just by writing an = after its type followed by the default you want to give it.
NULL has no equivalent in Swift. nil is also called nil in Swift. Nil has no equivalent in Swift. [NSNull null] can be accessed in Swift as NSNull()
Swift – Check if Variable is not nil If optional variable is assigned with nil , then this says that there is no value in this variable. To check if this variable is not nil, we can use Swift Inequality Operator != .
you can do like this but your strValue should be optional type
let strValue:String?
textfield.stringValue = strValue ?? "your default value here"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With