I am creating simple web-service client in Objective-C and found that creating separate class for each kind of web-service’s response could be overkill. Perhaps this because Objective-C has two files – H&M.
Here is sample of responses from web-service:
{"name": "John", status: "OK"}
or
{"total": "5500", status: "OK"}
or more complex
{"location": {"x": "140", "y": "90", "z": "0"},
"color": "red", status: "OK"}
I want to create separate class for each response, e.g. NameResponse, TotalResponse, LocationInfoResponse and subclass them from Response class.
I am sure in Java that would be quite compact to create some classes, but not sure about Objective-C.
UPDATE 1
One reason to use classes over NSDictionary is that code is more safe and better defined, e.g.:
I think
response.name
is better than
json valueForKey: "name"
Isn’t it?
Please put how would you do in the answer and is a class per each response is overkill in this case?
Having your data model clearly specified is never overkill. And formal data types are the best kind of specification – they’re already implemented!
If the web service responses are transferred between layers of your application, use classes. If the scope of their use is small enough, a NSDictionary might be acceptable.