Skip to content

Instantly share code, notes, and snippets.

@dfreniche
Created July 20, 2015 07:31
Show Gist options
  • Save dfreniche/cc6db49d53899f5d012d to your computer and use it in GitHub Desktop.
Save dfreniche/cc6db49d53899f5d012d to your computer and use it in GitHub Desktop.
import Foundation
// Not Optional Operator
// Useful to print out Optional values without the "Optional" text
prefix operator !? {}
prefix func !? (any: Any?) -> Any {
if let something = any {
return something
}
return ""
}
var s: String?
s = "hello"
println("Something \(s)") // Something Optional("hello")
println("Something \(!?s)") // Something hello
var i: Int?
i = 10
println("i \(i)") // i Optional(10)
println("i \(!?i)") // i 10
@dfreniche
Copy link
Author

Tired of printing optional values and seeing all those Optional() in your log files?

Just print saying that something is Not (!) Optional (?) and that's all!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment