Kwnstantinos Natsios Kwnstantinos Natsios - 12 days ago 13
Swift Question

Parse Error Handling swift 3

In Parse Documentation we can find this Session Error Handling in Swift.

// Swift
class ParseErrorHandlingController {
class func handleParseError(error: NSError) {
if error.domain != PFParseErrorDomain {
return
}

switch (error.code) {
case kPFErrorInvalidSessionToken:
handleInvalidSessionTokenError()

... // Other Parse API Errors that you want to explicitly handle.
}

private class func handleInvalidSessionTokenError() {

}
}

// In all API requests, call the global error handler, e.g.
let query = PFQuery(className: "Object")
query.findObjectsInBackgroundWithBlock { (objects: [AnyObject]!, error: NSError!) -> Void in
if error == nil {
// Query Succeeded - continue your app logic here.
} else {
// Query Failed - handle an error.
ParseErrorHandlingController.handleParseError(error)
}
}


Can someone help me make it to swift 3?

Till now I have done the following

import Foundation
import Parse

class ParseErrorHandlingController {
class func handleParseError(error: NSError) {
if error.domain != PFParseErrorDomain {
return
}

switch (error.code) {
case PFErrorCode.errorInvalidSessionToken.rawValue:
handleInvalidSessionTokenError() //error here again.

// Other Parse API Errors that you want to explicitly handle.
}


}
func handleInvalidSessionTokenError() {
PFUser.logOutInBackground { (error) -> Void in
if error == nil {
likesOfArray.removeAll()

}

}

}


And i call the function with the following code.

ParseErrorHandlingController.handleParseError(error: error as! NSError)


But i get the following error

Missing argument for parameter #1 in call


I'm really stucked !

Thanks a lot!

Answer

It looks like you declared handleInvalidSessionTokenError() inside your other function, in the Parse's example it's a private class function. About the error you could try to use case PFErrorCode.errorInvalidSessionToken.rawValue instead.

Comments