How to get the NSError message in iOS?

Normally you’ll want to use [error localizedDescription] to get the text to show to the user. Read the NSError documentation for more options. For simple logging when developing, you can do NSLog(@”Error: %@”, error). (That will give you ‘localizedDescription’ and everything else on your log in Xcode.)

What’s the difference between Error and NSError in Swift?

NSError is a Cocoa class An NSError object encapsulates information about an error condition in an extendable, object-oriented manner. It consists of a predefined error domain, a domain-specific error code, and a user info dictionary containing application-specific information. Error is a Swift protocol which classes, structs and enums can and NSError does conform to. A … Read more

swift programming NSErrorPointer error etc

These types and methods have changed a lot since Swift 1. The NS prefix is dropped The methods now throw exceptions instead of taking an error pointer Use of NSDictionary is discouraged. Instead use a Swift dictionary This results in the following code: do { let object = try JSONSerialization.jsonObject( with: responseData, options: .allowFragments) if … Read more

Swift creating NSError Object

Looks like you want (see dictionary keys): Swift 2 let userInfo: [NSObject : AnyObject] = [ NSLocalizedDescriptionKey : NSLocalizedString(“Unauthorized”, value: “Please activate your account”, comment: “”), NSLocalizedFailureReasonErrorKey : NSLocalizedString(“Unauthorized”, value: “Account not activated”, comment: “”) ] Swift 3 let userInfo: [AnyHashable : Any] = [ NSLocalizedDescriptionKey : NSLocalizedString(“Unauthorized”, value: “Please activate your account”, comment: “”) … Read more

Objective-C Exceptions

It is unsafe to throw exceptions in circumstances where resources are not automatically managed. This is the case of the Cocoa framework (and neighbor frameworks), as they use manual reference counting. If you throw an exception, any release call you skip over by unwinding the stack will result in a leak. This should limit you … Read more

Best Practice – NSError domains and codes for your own project/app

I personally use a reverse-DNS style domain. For example: NSError * myInternalError = [NSError errorWithDomain:@”com.davedelong.myproject” code:42 userInfo:someUserInfo]; The third part of the domain (@”myproject”) is just used to differentiate the errors from this project (“My Project”) from errors in another project (“My Other Project” => com.davedelong.myotherproject). It’s a simple way to ensure that I’m not … Read more

How can I use NSError in my iPhone App?

Well, what I usually do is have my methods that could error-out at runtime take a reference to a NSError pointer. If something does indeed go wrong in that method, I can populate the NSError reference with error data and return nil from the method. Example: – (id) endWorldHunger:(id)largeAmountsOfMonies error:(NSError**)error { // begin feeding the … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)