iOS: NSError (JSON) to NSMutableDictionary

1.9k Views Asked by At

I'm calling a block and I need convert the error (which returns a JSON) into NSDictionary.

CODE:

[endPoint updateModel:self.model withDomain:A_DOMAIN successBlock:^{

} errorBlock:^(NSError *error) {
    NSLog (@"Logging Error: %@", error);
}];

LOG:

2013-12-17 12:50:43.190 testApp[79103:70b]Logging Error: [Line 27] response string - [{"value":"sdfdsfewr","validator":"pattern","property":"profile.location.zip","expected":null,"message":"not a valid format ZIP code"}]

I would like to convert the *error that's into a NSDictionary? Thanks

2

There are 2 best solutions below

0
On

the log from the "error" object does not look like an NSError object, but instead looks like some sort of dictionary. maybe its a JSON filled NSString? In that case you will need to use NSJSONSerialization as described here to turn it into an NSDictionary:

NSJSONSerialization from NSString

1
On

From your response strings ie :

[{"value":"sdfdsfewr","validator":"pattern","property":"profile.location.zip","expected":null,"message":"not a valid format ZIP code"}].

It does not look like an NSError Object. But I am considering you got proper NSError object and to answer your question, there is pretty simple way to get dictionary out of NSError is as follows -

NSDictionary *jsonDictionary;
NSError *error = 'error to be converted to dictionary';

jsonDictionary = error.userInfo;

Hope this answers your question.