Explicit old error handling in Swift 2

Apple Introduces New Error Handling Procedure in Swift 2

https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/ErrorHandling.html#//apple_ref/doc/uid/TP40014216-CH7-ID10

I am working with a project that uses AFNetoworking v2.x, where AFHTTPRequestSerializer has 2 selectors:

- (NSMutableURLRequest *)requestWithMethod:(NSString *)method URLString:(NSString *)URLString parameters:(id)parameters DEPRECATED_ATTRIBUTE; - (NSMutableURLRequest *)requestWithMethod:(NSString *)method URLString:(NSString *)URLString parameters:(id)parameters error:(NSError * __autoreleasing *)error; 

First deprecated, but the second is automatically converted to the first signature by the Swift 2 compiler. The old-fashioned method is now not allowed:

 var error: NSError? let request = self!.operationManager.requestSerializer.requestWithMethod(method, URLString: url?.absoluteString, parameters: params, error: error) 

gives me a compile time error:

 Cannot convert value of type 'NSError?' to expected argument type '()' 

But the new notation reduces the error handling selector to an obsolete version without it.

 do { let request = try self!.operationManager.requestSerializer.requestWithMethod(method, URLString: url?.absoluteString, parameters: params) } catch let error as NSError { // ... } 

What is the best way to use Swift 2 to solve this problem? Is there a way to specify a specific selector in this situation?

UPD: a more accurate reference to the Swift 2 function that caused my problem. https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/BuildingCocoaApps/AdoptingCocoaDesignPatterns.html#//apple_ref/doc/uid/TP40014216-CH7-ID10

+6
source share
2 answers

This seems to work correctly in Xcode 7.1 (I remember that this problem is in 7.0) just changed your method to use the ie &error pointer

 var error: NSError? let request = self!.operationManager.requestSerializer.requestWithMethod(method, URLString: url?.absoluteString, parameters: params, error: &error) 
0
source

Swift 2 prefers to provide the Try Catch block only to the required methods. Thus, if your method has cases where you might need error handling, then only this block.

 do { try managedObjectContext.save() } catch { // Replace this implementation with code to handle the error appropriately. // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. let NSError = error as NSError NSLog("Unresolved error \(nserror), \(nserror.userInfo)") abort() } 
-1
source

All Articles