NSDictionary显示每个项目

时间:2011-04-09 05:20:19

标签: iphone objective-c xcode ios4

我有以下代码:

for (int i = 1; i<=count; i++) {

    NSString *j = [[NSString alloc] initWithFormat:@"%d",i ];

    NSArray *fields = [[NSArray alloc] initWithObjects:
                       @"name",nil];
    //@"title",@"mobile",@"email",@"website"
    [requestExecute setMethod:@"execute" withObjects:[NSArray arrayWithObjects:
                                                          @"testdb",userid,@"admin",@"res.partner.contact",@"read",j,fields,nil]];
    NSDictionary *records=[self executeXMLRPCRequest:requestExecute];

NSLog(@"\nKey : %@ value : \n",records);

输出看起来像

2011-04-09 12:34:32.431 Erp[1571:207]
 (  
        {  
        email = "benoit@yahoo.com";  
        id = 1;  
        mobile = 12345;  
        name = Mortier;  
        title = "Ms.";  
        website = "www.benoit.com";  
    }  
)  
2011-04-09 12:34:32.515 Erp[1571:207] (  
        {  
        email = "laurent@hotmail.com";  
        id = 2;  
        mobile = 23455;  
        name = Jacot;  
        title = "M.";  
        website = "www.laurent.com";  
    }  
)  
2011-04-09 12:34:32.599 Erp[1571:207] (  
        {  
        email = "thomas@yahoo.com";  
        id = 3;  
        mobile = 34567;  
        name = Passot;  
        title = "M.";  
        website = "www.thomas.com";  
    }  
)  
2011-04-09 12:34:32.683 Erp[1571:207] (  
        {  
        email = "etienne@yahoo.com";  
        id = 4;  
        mobile = 45678;  
        name = Lacarte;  
        title = Mss;  
        website = "www.etienne.com";  
    }  
)  
2011-04-09 12:34:32.767 Erp[1571:207] (  
        {  
        email = "chow@gmail.com";  
        id = 5;  
        mobile = 56789;  
        name = Tang;  
        title = Mss;  
        website = "www.chow.com";  
    }  
)  
2011-04-09 12:34:32.851 Erp[1571:207] (  
        {  
        email = "hudson@gmail.com";  
        id = 6;  
        mobile = 1237091;  
        name = Wong;  
        title = "M.";  
        website = "www.hudson.com";  
    }  
)  

这将逐个打印每条记录。现在我想逐一获取此记录中的每个字段。

2 个答案:

答案 0 :(得分:3)

Objective-C 2.0支持非常方便的快速枚举语法集合......

for(NSString *aKey in records){
    NSLog(@"%@", [[records valueForKey:aKey] description]);
}

答案 1 :(得分:0)

我不确定requestExecute是做什么的,但是如果你想循环遍历NSDictionary中的所有值,你可以使用[records allKeys]来返回字典中所有键的NSArray。