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.

vadian
  • 274,689
  • 30
  • 353
  • 361
Ludo
  • 743
  • 1
  • 10
  • 25
  • 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. – Thallius Nov 15 '15 at 13:13

6 Answers6

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
trojanfoe
  • 120,358
  • 21
  • 212
  • 242
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.

gnasher729
  • 51,477
  • 5
  • 75
  • 98
  • 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`. – trojanfoe Nov 15 '15 at 14:06
1

I solved it replacing:

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

to

NSError *error = nil;
javaboygo
  • 243
  • 2
  • 11
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" }];
Jayprakash Dubey
  • 35,723
  • 18
  • 170
  • 177
0

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)")
Yogesh Rathore
  • 147
  • 1
  • 7
-1

In Swift

I Solved this

 let error : NSError? = nil
M Murteza
  • 1,629
  • 15
  • 10
  • 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. – Eric Aya Aug 02 '21 at 08:45