Stumped on how to uinderstand why one form of a 'if let' works but another does not. Xcode 9.3
There is an Objective C file that declares a block type where the first parameter is an NSError. Without any nullability annotation, its viewed by Swift as an Optional.
typedef void(^MyError)(NSError *error, NSInteger type) ; // in ObjC header file
In Swift I see this is:
typealias MyError = (Error?, Int) -> Void
In the Swift file, in a MyError block:
if let err = error as NSError? {
// works
}
if let err = error {
// fails since err is of type Error - its not an NSError
}
if let err = error as? NSError {
// fails with cryptic message on Error conversion
}
So let me take a stab at this. `let err = error as NSError?` translates as, if the optional Error is actually a optional NSError AND is non-nil, then set err to this NSError. Its type is now NSError. [I sure don't remember using if let x = y as z? in the past - the trailing "?".
I poked around the Swift Programming Guide for a while, was unable to find this construct.