Enumerate NSDictionary with keys and objects, PHP style

后端 未结 3 683
暗喜
暗喜 2021-02-07 09:36

I know you can Enumerate the keys or values of NSMutableDictionary using NSEnumerator. Is it possible to do both together? I\'m looking for something s

3条回答
  •  一向
    一向 (楼主)
    2021-02-07 09:42

    NSDictionary* d = [NSDictionary dictionaryWithObjectsAndKeys:@"obj1",@"key1",
                                                               @"obj2",@"key2",
                                                               @"obj3",@"key3",
                                                               @"obj4",@"key4",nil];
    
    for (id key in [d allKeys]) {
        NSLog(@"%@ - %@",key,[d objectForKey:key]);
    }
    

    Outputs:

    keytest[7880:a0f] key3 - obj3
    keytest[7880:a0f] key1 - obj1
    keytest[7880:a0f] key4 - obj4
    keytest[7880:a0f] key2 - obj2
    

提交回复
热议问题