Alex Terente
Alex Terente

Reputation: 12036

How to write proper description method to a class?

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"

Upvotes: 24

Views: 14165

Answers (2)

user207616
user207616

Reputation:

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

Upvotes: 19

Alex Terente
Alex Terente

Reputation: 12036

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.

Upvotes: 13

Related Questions