handling error using xcode 7.0 beta in asynchronous block

227 views Asked by At

I am trying to validate different errors while downloading text files from AWS S3, and with the next piece of code:

... above here function receiving String parameters ruta, archivo, archivoLocal
let directorioURL = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).first! as NSURL
let archivoURL = directorioURL.URLByAppendingPathComponent("b\(archivoLocal)")
let downloadRequest = AWSS3TransferManagerDownloadRequest()
downloadRequest.bucket = ruta
downloadRequest.key = archivo
downloadRequest.downloadingFileURL = archivoURL

let transferManager = AWSS3TransferManager.defaultS3TransferManager()
let task = BFTask()
let executor = BFExecutor.mainThreadExecutor()
transferManager.download(downloadRequest).continueWithExecutor(executor, withBlock: { (task) -> AnyObject! in
if task.error != nil {
    if task.error.domain == AWSS3TransferManagerErrorDomain {
        self.processDomainErrorType(AWSS3TransferManagerErrorType(rawValue: task.error.code))
    } else {
        self.processError(task.error)
    }
} else if task.result != nil {
    do {
        let mytext = try String(contentsOfURL: archivoURL, encoding: NSUTF8StringEncoding)
        self.processResult(mytext)
    } catch let urlerror as? NSError {
        self.processError(urlerror)
    }
}
...

I am getting the error:

Invalid conversion from throwing function of type '(_) throws -> AnyObject!' to non-throwing function type '@convention(block) (BFTask!) -> AnyObject!'

I obtained the "do { try } catch" syntax from https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/BuildingCocoaApps/AdoptingCocoaDesignPatterns.html#//apple_ref/doc/uid/TP40014216-CH7-ID10

I can remove the error by replacing the catch clause with:

    } catch _ {
        self.processError(NSError(domain: "String-ContentsOfURL Error", code: 100, userInfo: nil))
    }

Of course this way I will never know the real cause why ContentsOfURL could be failing.

All I can figure out why this error happens is because this syntax is valid only for OS X apps and for iOS the error handling guide at https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/ErrorHandling.html#//apple_ref/doc/uid/TP40014097-CH42 allows only the second syntax unless you own the object throwing the errors from an enum structure of ErrorType type, which is not the case since I want to catch the NSError from String object, contentsOfURL function.

I hope someone could guide me through this, maybe being XCode 7 a beta, the catch syntax is still incomplete or maybe I should not matter about the reason why this function fails, but I think it is important to determine what is making this function fail and if it could be traced and fixed before reaching the do-try-catch clause.

Additionally to the above error, I am getting a warning in the task variable assignation line to BFTask() saying that "Initialization of immutable value 'task' was never used". I think this is a bug with this beta version that it doesn't include the pattern to acknowledge that the variable task is being used in the asynchronous block. I'd appreciate a lot some confirmation about this and if I just need to ignore it.

By the way, the only reason I am using XCode 7 beta is because my client wants to evaluate the app before acquiring their apple membership.

1

There are 1 answers

0
Gerd Castan On

Apple replaced NSError with ErrorType in Swift 2.

Replace your own explicit usage of NSError with ErrorType and you don't get this type of compiler errors.