Why does [[NSError alloc] init]; in Xcode throw an error?
Asked Answered
R

6

7

I have the following code in Xcode :

NSError *error = [[NSError alloc] init];
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

And it throws following error in the logs

[NSError init] called; this results in an invalid NSError instance. It will raise an exception in a future release. Please call errorWithDomain:code:userInfo: or initWithDomain:code:userInfo:. This message shown only once.

Maybe, you will tell me that the answer is in the log, but I do not understand how to init NSError.

Roath answered 15/11, 2015 at 13:11 Comment(1)
Because sendSynchronousRequest will alloc the error by itself. thats why you just give a pointer to the error (*). So the Instanz you created will be never used and is invalid.Benedikt
E
13

You are not allowed to create an NSError instance via -init; use -initWithDomain:code:userInfo: instead or the constructor method +errorWithDomain:code:userInfo:.

In your case it's redundant anyway as that method will create it in the case of error.

This is the normal pattern for using it:

NSError *error = nil;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request
                                      returningResponse:&response
                                                  error:&error];
if (!urlData) {
    NSLog(@"Error: %@", [error localizedDescription]);
    return NO;
}

// Successful
Event answered 15/11, 2015 at 13:13 Comment(1)
You mean nil not nul I suppose,Roath
S
1

Think about what you are doing. You should initialise an NSError* variable by setting it to nil. You don't even have to do that, the compiler does it for you. Initialising it by creating a new NSError object is nonsense. It's the same nonsense that you often see when beginners write

NSArray* array = [[NSArray alloc] init];
array = ...;

In the case of NSError, Cocoa rightfully tells you that creating an NSError object without any error information is nonsense and therefore a bug. But there is not the slightest need to do this. Actually, it would break the line that you missed out where you check if error == nil.

Spavined answered 15/11, 2015 at 14:4 Comment(1)
You are not supposed to check for error with if (error != nil) { ... }. You are supposed to check the return value from the method and then use error.Event
S
1

I solved it replacing:

NSError *error = [[NSError alloc]init];

to

NSError *error = nil;
Supervision answered 17/2, 2016 at 10:5 Comment(0)
P
0

The log itself states that you should use errorWithDomain:code:userInfo: or initWithDomain:code:userInfo: in order to resolve this issue.

Use of -[NSError init] is not recommended and it can cause exception in future release.

Screenshot for warning

Screenshot for usage 1

Screenshot for usage 2

Example:

    NSError *errMsg = [NSError errorWithDomain:@"domain" code:1001 userInfo:@{  
                      NSLocalizedDescriptionKey:@"Localised details here" }];
Ply answered 18/4, 2016 at 6:53 Comment(0)
M
-1

In Swift

I Solved this

 let error : NSError? = nil
Milissa answered 2/8, 2021 at 6:22 Comment(1)
This will never work: an optional declared with let cannot change its value. And anyway, the issue in the question does not apply to Swift, the method signature is different. This answer is wrong and off-topic.Propensity
E
-1

Use the following code for NSError in Swift

let error = NSError(domain: "", code: 101, userInfo: [ NSLocalizedDescriptionKey: "error in download"])

and use this error

  print("Domain : \(error.domain)")
  print("code : \(error.code)")
  print("Description : \(error.localizedDescription)")
Endive answered 15/12, 2021 at 17:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.