It seems that NSFileCoordinator, despite being a pretty important class, has not been given any Swiftification. It has not been given any async
alternative; you have to provide a completion handler. Moreover, it takes an NSErrorPointer rather than being marked throws
, and the pointee cannot be referred to inside the completion handler:
let url = URL(fileURLWithPath: "fakepath")
var error: NSError?
NSFileCoordinator().coordinate(readingItemAt: url, error: &error) { url in
let theError = error
print(theError as Any)
}
This results in an error: "Simultaneous accesses to error
, but modification requires exclusive access."
If I can't access the error from inside the block, what use is it? How am I supposed to access it? Surely not after the block; the completion handler is called asynchronously (because we have to wait until coordinated access is possible), so after the block, the error will always be nil
even if there was in fact an error.
(I notice that Apple's own example code skirts this issue by never checking the value of its error
variable at all.)
error
after the call tocoordinate(readingItemAt: ...)
. (Also, I don't know why Apple suggests to use a sentinel value when you can just check if the error is nil or not. Maybe it's possible forerror
to still be nil if the coordination fails for some obscure reason?) – Solingen