Skip to content

Instantly share code, notes, and snippets.

@AfricanSwift
Last active June 7, 2016 10:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AfricanSwift/6ed839400c4fec442db411371f0487da to your computer and use it in GitHub Desktop.
Save AfricanSwift/6ed839400c4fec442db411371f0487da to your computer and use it in GitHub Desktop.
=∅ (null check) debug operator
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