1

I want to improve my class description function by allowing it to display nested custom objects in a neat JSON style.

Currently I do this:

- (NSString *)description
{
    NSDictionary *dict = @{
                           @"streetName": self.streetName ? : @"",
                           @"streetNumberArray": self.streetNumberArray ? : @"",
                           @"otherInfo": self.otherInfo ? : @"",
                           @"startTime" : self.startTime ? : @"",
                           @"endTime": self.endTime ? : @"",
                           @"startWeekday": self.startWeekday ? : @"",
                           @"address" : self.address ? : @"",
                           @"type" : self.type ? : @"",
                           @"parkingDistrict" : self.parkingDistrict ? : @""
                           };
    return [dict description];
}

But I want to add my custom object property (which itself has a similar description method.) so I get something like this when I print out my custom parent object.

  {
   "users": [
  {
      "userId": 1,
      "firstName": "Acting user",
      "lastName": "Number Uno",
      "email": "acting1@gmail.com",
      "accountId": 1,
      "friends" : [
        {
          "userId": 2,
          "firstName": "Acting user",
          "lastName": "Number Dos",
          "email": "acting2@gmail.com",
          "accountId": 2,
          "appKey": "097c38869f72114aee3f58dced9fdddf7ce703b92947"
        }
        ]
  }
]
}

How can I do that?

Rafał Sroka
  • 39,540
  • 23
  • 113
  • 143
Sebastian L
  • 924
  • 2
  • 10
  • 20
  • Maybe this answer will be helpful: http://stackoverflow.com/questions/6368867/generate-json-string-from-nsdictionary-in-ios. You could use "pretty print" option. – schmidt9 Mar 08 '16 at 21:13

0 Answers0