24

How to write proper description method to a class?

I have implemented

- (NSString *)description {
     NSString *descriptionString = [NSString stringWithFormat:@"Name: %@ \n Address: %@ \n", self.name, self.address];
     return descriptionString;

}

Evrey thing is fine if I call description on my object. But if I have an array of objects and I call description on it I get:

"Name: Alex \n Address: some address \n",

What I would like to get is

"Name: Alex

Address: some address"

Alex Terente
  • 12,006
  • 5
  • 51
  • 71
  • 1
    you will have to loop over the elements and call `description` on each. Array description is formatted output, which may short-circuit some things like "\n", to keep formatting consistent –  Jan 26 '12 at 10:23
  • 1
    see [http://stackoverflow.com/a/1828689/971401](http://stackoverflow.com/a/1828689/971401). Answers there can probably help you. –  Jan 26 '12 at 10:52

2 Answers2

19

Additionally you can use a carriage return \r which will end up in a new line too (even in a NSArray description)

13

I dig a little more in iOS frameworks and I have observed that the default behavior of the iOS sdk description is not to place "\n" but ";".

Example:

    UIFont *font = [UIFont systemFontOfSize:18];
    NSLog(@"FontDescription:%@",[font description]);

    NSMutableArray *fontsArray = [NSMutableArray arrayWithCapacity:0];
    for(int index = 0; index < 10; index++) {
        [fontsArray addObject:font];
    }
    NSLog(@"FontsArrayDescription:%@",[fontsArray description]);

The out put is:

FontDescription: font-family: "Helvetica"; font-weight: normal; font-style: normal; font-size: 18px

FontsArrayDescription:(

 "<UICFFont: 0x6e2d8b0> font-family: \"Helvetica\"; font-weight: normal; font-style: normal; font-size: 18px",
 "<UICFFont: 0x6e2d8b0> font-family: \"Helvetica\"; font-weight: normal; font-style: normal; font-size: 18px",
 "<UICFFont: 0x6e2d8b0> font-family: \"Helvetica\"; font-weight: normal; font-style: normal; font-size: 18px",
 "<UICFFont: 0x6e2d8b0> font-family: \"Helvetica\"; font-weight: normal; font-style: normal; font-size: 18px",
 "<UICFFont: 0x6e2d8b0> font-family: \"Helvetica\"; font-weight: normal; font-style: normal; font-size: 18px",    
 "<UICFFont: 0x6e2d8b0> font-family: \"Helvetica\"; font-weight: normal; font-style: normal; font-size: 18px",
 "<UICFFont: 0x6e2d8b0> font-family: \"Helvetica\"; font-weight: normal; font-style: normal; font-size: 18px",
 "<UICFFont: 0x6e2d8b0> font-family: \"Helvetica\"; font-weight: normal; font-style: normal; font-size: 18px",
 "<UICFFont: 0x6e2d8b0> font-family: \"Helvetica\"; font-weight: normal; font-style: normal; font-size: 18px",
 "<UICFFont: 0x6e2d8b0> font-family: \"Helvetica\"; font-weight: normal; font-style: normal; font-size: 18px"  

)

So I have decided to use the same approach with my class.

- (NSString *)description {
     NSString *descriptionString = [NSString stringWithFormat:@"Name: %@; Address: %@;", self.name, self.address];
     return descriptionString;

}

And the out put will be:

"Name: Alex; Address: some address;"

For object it self.

objecsArrayDescription:(

 "Name:Alex; Address: some address;",
 "Name:Alex; Address: some address;",
 "Name:Alex; Address: some address;",
 "Name:Alex; Address: some address;",
 "Name:Alex; Address: some address;",
 "Name:Alex; Address: some address;",
 "Name:Alex; Address: some address;",
 "Name:Alex; Address: some address;",
 "Name:Alex; Address: some address;",
 "Name:Alex; Address: some address;" 

)

For an array of objects.

Alex Terente
  • 12,006
  • 5
  • 51
  • 71