How to write proper description method to a class?

13,931

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.

Share:
13,931
Alex Terente
Author by

Alex Terente

Developer @IDT and founder of TAGonSoft.

Updated on July 05, 2022

Comments

  • Alex Terente
    Alex Terente almost 2 years

    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"