2009-11-19 45 views
0
groupContentList = [[NSArray alloc] initWithObjects: 
        [Product productWithType:@"Device" name:@"iPhone"], 
        [Product productWithType:@"Device" name:@"iPod"], 
        [Product productWithType:@"Device" name:@"iPod touch"], 
        [Product productWithType:@"Desktop" name:@"iMac"], 
        [Product productWithType:@"Desktop" name:@"Mac Pro"], 
        [Product productWithType:@"Portable" name:@"iBook"], 
        [Product productWithType:@"Portable" name:@"MacBook"], 
        [Product productWithType:@"Portable" name:@"MacBook Pro"], 
        [Product productWithType:@"Portable" name:@"PowerBook"], nil]; 

如何打印groupcontestList問題有關的NSArray

回答

1
For(Product* prod in groupContent){ 
    NSLog(@"type=%@ name=%@", prod.type, prod.name); 
} 
4

的值可以檢索表示NSArray內容與-description方法的字符串。這隱含與用於:

NSLog(@"%@", groupContentList); 

它將依次invoque在其每個元件的-description方法(默認爲對象的打印地址作爲NSObject定義)。

因此,如果您希望它可用,則必須爲Product類定義-description方法。