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
9
votes
1 answer

how to get the message in NSLocalizedDescription in iOS?

I have this error in requesting data from api.. All I want is to get only the message "The request timed out." in NSLocalizedDescription. How to get that? heres the return error below. Error Domain=NSURLErrorDomain Code=-1001 "The request timed…
user2749248
  • 616
  • 7
  • 15
8
votes
1 answer

Obj-C: NSError in initialiser

Fairly simple question: I have an init method on my class that has the potential to go wrong. If it does, I plan to "return nil", but I would also like to return an error. Is it bad practice to have an NSError** parameter to an init method? My…
dark_perfect
  • 1,438
  • 22
  • 38
8
votes
2 answers

why is "error:&error" used here (objective-c)

why is "error:&error" used here (objective-c) NSError *error = nil; NSArray *array = [moc executeFetchRequest:request error:&error]; wouldn't an object in objective-c be effectively pass-by-reference anyway?
Greg
  • 31,898
  • 75
  • 232
  • 424
8
votes
1 answer

Why would Error always be NSError?

I have the following class defined on a Playground with Swift 3: class MyError: Error { } Then, I create an instance of such class and check if it is a NSError let firstError = MyError() firstError is NSError // Output: false The output is as…
Matias
  • 503
  • 5
  • 21
8
votes
3 answers

iOS 10, call directory extension not debugged

I am working on Call directory extension with callkit. I selected Call directory extension for debug and when I run this target, no NSLog is shown. How can I debug this extension? ps : When I run this extension, Debug -> Attach to process -> no…
Lisa
  • 171
  • 7
8
votes
3 answers

Use of unresolved identifier when using StoreKit constants with iOS 9.3/Xcode 7.3

I get the error "Use of unresolved identifier" when trying to use one of these StoreKit constants: SKErrorClientInvalid SKErrorPaymentCancelled SKErrorPaymentInvalid SKErrorPaymentNotAllowed SKErrorStoreProductNotAvailable SKErrorUnknown Your code…
JAL
  • 39,073
  • 22
  • 153
  • 285
8
votes
2 answers

ErrorType in swift - how do I get an error code out?

I am passed an error - its an ErrorType - in a completion. I can 'po' it in the debugger, but how do I get the number -1009 out in swift code. The only call I can find to make is 'debugDescription'. Is there a Dictionary in there? Whoever made the…
Tom Andersen
  • 6,788
  • 2
  • 34
  • 53
8
votes
4 answers

Is it a good practice to extend NSError

Sorry for asking this question. I know in java we are extending Exception class for custom exceptions. But I don't see any scenarios for that in objective c. So my question, Is it a good practice to extend NSError and introducing custom errors? If…
uiroshan
  • 4,351
  • 1
  • 36
  • 36
8
votes
3 answers

NSError localizedDescription always returns english error message

Why does an NSError * error object with NSURLErrorDomain always return the error.localizedDescription in english ?
d.ennis
  • 3,375
  • 2
  • 25
  • 35
8
votes
3 answers

Update NSError UserInfo

I'm trying to update an NSError object with more information. For example, an api call may fail and I want to update the error object returned from the api class with view controller information (method name that caused error, client message, any…
JeffRegan
  • 1,302
  • 9
  • 24
7
votes
3 answers

CKError localizedDescription

Aim I would like to display the CKError encountered to the user in the app as an alert. So I would like to extract the string from the error that can be displayed to the user. Note: This question is not about UI code to display. Just want to…
user1046037
  • 14,608
  • 12
  • 79
  • 117
7
votes
1 answer

How to use RecoverableError to retry?

In Swift 3, the RecoverableError protocol was introduced, but here is very little documentation on how to use this. This sounds like a native way to offer retry functionality for failed processes, which could be pretty useful. What's an example of…
TruMan1
  • 27,054
  • 46
  • 150
  • 273
7
votes
2 answers

Check NSError code in swift

I'm trying to check the error code value in Swift, and find myself a bit confused by the new struct types and conversions. What I want to do is simply take an NSError object passed in a handler closure and check its code type by comparing it to a…
KPM
  • 10,201
  • 3
  • 41
  • 63
7
votes
4 answers

Less verbose way to log errors in Objective-C

Many Cocoa methods take an optional NSError ** argument that they use to report errors. Frequently, I find myself using such methods even in circumstances where the only way that an error could occur is through programming error on my part, rather…
Mark Amery
  • 110,735
  • 57
  • 354
  • 402
6
votes
3 answers

NSError: Does using nil to detect Error actually turn off error reporting?

I got into the habit of coding my error handling this way: NSError* error = nil; NSDictionary *attribs = [[NSFileManager defaultManager] removeItemAtPath:fullPath error:&error]; if (error != nil) { DLogErr(@"Unable to remove file: error %@,…
EtienneSky
  • 1,096
  • 9
  • 17
1 2
3
20 21