How to write proper description method to a class?
Additionally you can use a carriage return \r
which will end up in a new line too (even in a NSArray description)
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.