Ola,
I don't have to much exp with the Swift language. I want to print into a String some variables and some of them are optional.
func onRegPin(timeOut: Int64, pin: String?)->()
{
String(format: "Timeout: %d Pin: %@", timeOut, pin!)
}
If the pin is nil at runtime I got some assert or something.
Is there a way to print this optional parameters using String ?
You can do this:
pinValue
is an empty string if pin
is nil
otherwise the unwrapped pin
value
func onRegPin(timeOut: Int64, pin: String?)->()
{
let pinValue = pin ?? ""
String(format: "Timeout: %d Pin: %@", timeOut, pinValue)
}
func onRegPin(timeOut: Int64, pin: String?)->()
{ if let pin = pin{
String(format: "Timeout: %d Pin: %@", timeOut, pin)
}else{
String(format: "Timeout: %d No Pin ", timeOut)
}
}
UPDATE: Using default Parameter
func onRegPin(timeOut: Int64, pin:String?="No pin"){
String(format: "Timeout: %d Pin: %@", timeOut, pin!)
}
If pin is nil call function only with timeOut
onRegPin(3423)
else if pin is not nil:
onRegPin(3423, pin:"Pin")
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