-(void)resultaat{
for(id key in [addressbook allKeys]){
NSLog(@"\n%@ \n-\n %@ ", key,[addressbook objectForKey:key] );
}
}
I have a method resultaat which loops over a mutable dictionary, this dictionary has the alfabet as keys and one or more person object(s) in an array as value. The personobject has a name, multiple emails, multiple phone numbers.
In the person class I used description to join the mails and phone numbers into comma-separated strings and output the results.
-(NSString *) description {
NSString *joinedmails = [mails componentsJoinedByString:@","];
NSString *joinedtelnrs = [telnrs componentsJoinedByString:@","];
return [NSString stringWithFormat:@"\n Naam: %@ \n email: %@ \n telefoonnr: %@",naam,joinedmails, joinedtelnrs];
}
This works fine if I do e.g.
NSLog(@"%@",waldo);
I get
2012-02-27 17:39:59.355 Adresboek[5162:503]
Naam: Waldo Odlaw
email: waldo.odlaw@waldo.com,waldo.odlaw@gmail.com
telefoonnr: 123456789,987654321
But when I loop over the entire adressboek the person object is not returned like above
-(void)resultaat{
for(id key in [addressbook allKeys]){
NSLog(@"\n%@ \n-\n %@ ", key,[addressbook objectForKey:key] );
}
}
2012-02-27 17:39:59.407 Adresboek[5162:503]
w
-
(
"\n Naam: Waldo Odlaw \n email: waldo.odlaw@waldo.com,waldo.odlaw@gmail.com \n telefoonnr: 123456789,987654321",
"\n Naam: Wuno Odlaw \n email: wuno.odlaw@wuno.com,wuno.odlaw@gmail.com \n telefoonnr: 2468101214,1412108642"
)
2012-02-27 17:39:59.408 Adresboek[5162:503]
j
-
(
)
How can I format my output.
The documentation for the NSArray class’s
-descriptionmethod says it will return a description of the array formatted as a property list. I suspect that its implementation is taking the -description from your PersonObject and converting all of the newline characters to a ‘/’ character followed by a ‘n’ character. That way it shows the newline in your strings while still remaining a valid property list format.To get what you want I think you will need to manually loop through the array to build up a string that looks the way you want it. If you want to print the whole address book using the %@ format specifier, then you would probably need to subclass NSArray and provide your own
-descriptionmethod for the arrays you store in your dictionary.