-
-
Save AfricanSwift/6ed839400c4fec442db411371f0487da to your computer and use it in GitHub Desktop.
=∅ (null check) debug operator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
infix operator =∅ {} | |
func =∅<T> (v:T?, ref: (file: String, line: Int)) -> T? { | |
#if debug | |
var unwrap = false | |
if let _ = v { unwrap = true } | |
let source = ref.file | |
.characters | |
.split("/") | |
.map{String($0)} | |
.last ?? "" | |
let outcome = "unwrap(\(unwrap ? "success" : "failure"))" | |
print("\(source)(\(ref.line)), \(outcome), value: \(v)") | |
#endif | |
return v | |
} | |
let dict: [String: Any] = ["Apple": "one", "Google": "two", "Microsoft": 3] | |
guard | |
let apple = dict["Apple"] as? String =∅ (#file, #line), | |
let google = dict["Google"] as? String =∅ (#file, #line), | |
let microsoft = dict["Microsoft"] as? String =∅ (#file, #line) | |
else { | |
fatalError("unwrap failed") | |
} | |
print(apple, google, microsoft) | |
// Example output: | |
// playground218.swift(114), unwrap(success), value: Optional("one") | |
// playground218.swift(115), unwrap(success), value: Optional("two") | |
// playground218.swift(116), unwrap(failure), value: nil |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment