3

How to Convert an NSstring to NSerror object type .

For Example

NSString *msg;
msg  = "ERROR - Error Domain=Handling Login Response Code=20017 "Invalid user name" UserInfo={NSLocalizedDescription=Invalid user name} errorCode 20017 . "

I want to convert the nsstring[msg] into NSerror Type, because I want to print "Invalid user name" using error.localizedDescription

Paulw11
  • 108,386
  • 14
  • 159
  • 186
athira
  • 31
  • 5
  • 1
    You can't really unmake the sausage. It looks like you already had an NSError somewhere but you now have the string value. Or do you want to create an `NSError` object? In which case you would need to [create one](https://developer.apple.com/reference/foundation/nserror?language=objc) – Paulw11 Apr 11 '17 at 03:36
  • I agree with @Paulw11. Do not use `-description` to recreate something. That's bad behavior. Apple could change it, for your custom objects, you may also change it. It's bad habit. – Larme Apr 11 '17 at 08:02
  • Does this answer your question? [How can I use NSError in my iPhone App?](https://stackoverflow.com/questions/4654653/how-can-i-use-nserror-in-my-iphone-app) – Top-Master Sep 12 '21 at 00:23

1 Answers1

3

I believes you want to create your own NSError object.

NSError *error = [NSError errorWithDomain:@"com.yourcompany.appname" code:3456 userInfo:@{NSLocalizedDescriptionKey:@"Invalid user name."}];
NSLog(@"Error: %@", error.localizedDescription);
Hemang
  • 26,840
  • 19
  • 119
  • 186