1

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

user1107173
  • 10,334
  • 16
  • 72
  • 117
  • What kind of object is `endPoint`, and where does the `updateModel:withDomain:` method come from? – Martin R Dec 17 '13 at 21:05
  • I believe endPoint is a block used to talk to the backend in JSON. – user1107173 Dec 17 '13 at 21:44
  • What framework do you use to talk to the backend? - What you see in the NSLog output is the error *description*. With more information about the used methods it might be possible to extract the JSON part. – Martin R Dec 17 '13 at 21:45
  • 1
    You need to strip off "[Line 27] response string -" before converting it from JSON. – Hot Licks Dec 17 '13 at 21:52
  • Hot Licks. That did the trick. Thanks. If you want to put in the as an answer, I'll mark it as correct. – user1107173 Dec 18 '13 at 17:04

2 Answers2

1

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

Community
  • 1
  • 1
tjboneman
  • 668
  • 4
  • 8
0

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.

iLearner
  • 1,670
  • 2
  • 19
  • 45
  • This answer is not needed at this time because its asked in 2013. Also it doesn't describe the problem correctly. And OP already got the correct answer in comments section if you can see. – TheTiger Mar 21 '18 at 06:28