Accessing Code in Swift 3 Error

Accessing code in Swift 3 Error

Casting to SKError seems to be working for me in xCode 8 and Swift 3...

    guard let error = transaction.error as? SKError else {return}
switch error.code { // https://developer.apple.com/reference/storekit/skerror.code
case .unknown: break
case .paymentCancelled: break
case .clientInvalid: break
case .paymentInvalid: break
case .paymentNotAllowed: break
case .cloudServiceNetworkConnectionFailed: break
case .cloudServicePermissionDenied: break
case .storeProductNotAvailable: break
}

No need for rawValue.

Error.code no longer valid syntax in swift 3?

Try casting error to NSError

func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
if (error as NSError).code == 3010 {
print("Push notifications are not supported in the iOS Simulator.\n")
} else {
print("application:didFailToRegisterForRemoteNotificationsWithError: %@\n", error)
}
}

Generate your own Error code in swift 3

You can create a protocol, conforming to the Swift LocalizedError protocol, with these values:

protocol OurErrorProtocol: LocalizedError {

var title: String? { get }
var code: Int { get }
}

This then enables us to create concrete errors like so:

struct CustomError: OurErrorProtocol {

var title: String?
var code: Int
var errorDescription: String? { return _description }
var failureReason: String? { return _description }

private var _description: String

init(title: String?, description: String, code: Int) {
self.title = title ?? "Error"
self._description = description
self.code = code
}
}

switch(error!.code) in canEvaluatePolicy function does not exist in Swift 3 (Xcode 8)

First change your reply closure of evaluatePolicy method, in Swift 3 it is Error not NSError.

reply: { (success : Bool, error : Error? ) -> Void in

Second, change performSegue with identifier like this.

performSegue(withIdentifier: "dismissLogin", sender: self)

In Swift 3 you need to convert Error object to NSError or use _code with Error instance instead of code.

switch((error! as NSError).code)

OR

switch(error!._code)

You need to also change you dispatch syntax like this.

Dispatch.async.main {
//write code
}

Swift 3 error when accessing NSUserDefaults, why?

This is probably the first time launching the app and you have not yet set anything in user defaults for "oldDate". This will only be nil the first time a user uses your app.

In the file AppDelegate.swift you could add the code below to didFinishingLaunchingWithOptions

  func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
if UserDefaults.standard.object(forKey: "oldDate") == nil {
UserDefaults.standard.setValue(Date(), forKey: "oldDate")
}
return true
}

swift 3 Type 'Any' error

What about something like:

let values = snapshot.value as! Dictionary<String,String>
username = values["username"]
...


Related Topics



Leave a reply



Submit