Questions tagged [nserror]

NSError is a Cocoa class that encapsulates richer and more extensible error information than is possible using only an error code or error string.

An NSError object encapsulates richer and more extensible error information than is possible using only an error code or error string. The core attributes of an NSError object are an error domain (represented by a string), a domain-specific error code and a user info dictionary containing application specific information.

Several well-known domains are defined corresponding to Mach, POSIX, and OSStatus errors. Foundation error codes are found in the Cocoa error domain and documented in the Foundation Constants Reference. In addition, NSError allows you to attach an arbitrary user info dictionary to an error object, and provides the means to return a human-readable description for the error.

NSError is not an abstract class, and can be used directly. Applications may choose to create subclasses of NSError to provide better localized error strings by overriding -localizedDescription.

In general, a method should signal an error condition by—for example—returning NO or nil rather than by the simple presence of an error object. The method can then optionally return an NSError object by reference, in order to further describe the error.

NSError is toll-free bridged with its Core Foundation counterpart, CFError.

Resources

303 questions
230
votes
9 answers

How can I use NSError in my iPhone App?

I am working on catching errors in my app, and I am looking into using NSError. I am slightly confused about how to use it, and how to populate it. Could someone provide an example on how I populate then use NSError?
Nic Hubbard
  • 39,231
  • 60
  • 236
  • 403
116
votes
3 answers

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

There is a previous SO post regarding setting up error domains for your own frameworks, but what is the best practice regarding setting up error domains and custom error codes for your own project/app? For example, supposing you're working on a Core…
Neal L
  • 4,259
  • 8
  • 31
  • 39
96
votes
11 answers

HTTP status code 0 - Error Domain=NSURLErrorDomain?

I am working on an iOS project. In this application, I am downloading images from the server. Problem: While downloading images I am getting Request Timeout. According to documentation HTTP status code of request timeout is 408. But in my…
Irfan DANISH
  • 7,467
  • 10
  • 35
  • 63
56
votes
7 answers

Objective-C Exceptions

I have just completed an iPhone app programming course. As part of the course, I saw Objective-C provides exception handling using the @try directive The system library does not use exception handling, preferring to return nil I asked if I should…
Adrian Smith
  • 15,946
  • 11
  • 65
  • 89
45
votes
3 answers

Swift creating NSError Object

I am trying to create an error object to display to the user. let userInfo: [NSObject : AnyObject] = [ "NSLocalizedDescriptionKey" : NSLocalizedString("Unauthorized", comment: ""), "NSLocalizedFailureReasonErrorKey" :…
Ehab Saifan
  • 489
  • 1
  • 4
  • 8
43
votes
4 answers

swift programming NSErrorPointer error etc

var data: NSDictionary = NSJSONSerialization.JSONObjectWithData(responseData, options:NSJSONReadingOptions.AllowFragments, error: error) as NSDictionary; This line of code gives me error NSError is not convertable to NSErrorPointer. So I then…
user3732493
  • 443
  • 1
  • 4
  • 8
42
votes
5 answers

Returning an NSString from an NSError

I am using the NSURLRequest class in my iPhone app, and the method that calls it returns an NSString which is great for when the connection goes through ok, but the issue is I need to convert the NSError into an NSString so I can either return it…
tarnfeld
  • 23,722
  • 39
  • 106
  • 145
35
votes
1 answer

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

I am creating a library that should return errors so I'm wondering which one should use for my purposes. UPDATE: I should clarify, the returned result will be from a asynchronous call so I need to inform to the user if there was an error and I…
Wilson
  • 8,463
  • 3
  • 36
  • 42
34
votes
3 answers

Why is `&` (ampersand) put in front of some method parameters?

I'ver wondered, why is it that in front of an NSError, such as below, do we put: &error and not error? E.g. NSArray *result = [managedObjectContext executeFetchRequest:fetchRequest error:&error]; Hope you can explain, also is this always the way or…
Josh Kahane
  • 15,834
  • 40
  • 127
  • 241
33
votes
4 answers

How to get the NSError message in iOS?

I am having the method in my view controller as shown below: - (void)parser:(PaymentTermsLibxmlParser *)parser encounteredError:(NSError *)error { NSLog("error occured"); } Here I need to show the Actual error message in the NSError in my alert…
Monish Kumar
  • 2,610
  • 4
  • 34
  • 53
26
votes
2 answers

Swift doesn't convert Objective-C NSError** to throws

I have some Objective-C legacy code, that declares method like - (void)doSomethingWithArgument:(ArgType)argument error:(NSError **)error As written here…
Anton Tyutin
  • 576
  • 4
  • 15
25
votes
1 answer

Value of type 'Error' has no member 'code'

I am moving a test application over to Xcode 8 Beta 5 and converted my code to Swift 3. I am left with a few errors regarding the switch to Error from NSError. I get an error in Xcode saying, 'Value of type 'Error' has no member 'code'. I can still…
Dan Levy
  • 3,241
  • 1
  • 23
  • 43
20
votes
3 answers

Swift: Corelocation handling NSError in didFailWithError

I'm using CoreLocation to successfully determine the user's location. However when i try to use the CLLocationManagerDelegate method: func locationManager(_ manager: CLLocationManager!, didFailWithError error: NSError!) I run into problems with the…
Magnas
  • 3,507
  • 4
  • 27
  • 40
20
votes
9 answers

How to detect and handle HTTP error codes in UIWebView?

I want to inform user when HTTP error 404 etc is received. How can I detect that? I've already tried to implement - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error but it is not called when I receive 404 error.
EEE
  • 4,498
  • 3
  • 25
  • 34
18
votes
5 answers

Swift, NSJSONSerialization and NSError

The problem is when there is incomplete data NSJSONSerialization.JSONObjectWithData is crashing the application giving unexpectedly found nil while unwrapping an Optional value error instead of informing us using NSError variable. So we are unable…
Hope
  • 1,520
  • 3
  • 17
  • 32
1
2 3
20 21