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
How to Update the Constant Height Constraint of a Uiview Programmatically
Swift - Resolving a Math Operation in a String
Xcode 11 & Ios13, Using Uikit Can't Change Background Colour of Uiviewcontroller
Get Class Name of Object as String in Swift
How to Set Layer Cornerradius For Only Bottom-Left, Bottom-Right, and Top-Left Corner
Prevent Dismissal of Uialertcontroller
Can You Execute an Applescript Script from a Swift Application
First Item in a List Is Always Selected
Why Is an Observedobject Array Not Updated in My Swiftui Application
Structure VS Class in Swift Language
How to Create Usdz File Using Xcode Converter
How to Store 1.66 in Nsdecimalnumber
How to Make a Weak Protocol Reference in 'Pure' Swift (Without @Objc)
How to Round a Double to the Nearest Int in Swift
Custom Back Button For Navigationview'S Navigation Bar in Swiftui