Example of [NSDictionary getObjects:andKeys:]

前端 未结 2 595
暖寄归人
暖寄归人 2021-02-15 16:38

I couldn\'t find a working example of the method [NSDictionary getObjects:andKeys:]. The only example I could find, doesn\'t compile. I provided the errors/warnings

相关标签:
2条回答
  • 2021-02-15 17:24

    Under ARC the solution needs to be modified as follows (__unsafe_unretained added to the array definitions):

    NSDictionary *myDictionary = [NSDictionary dictionaryWithObjectsAndKeys:@"1", @"A", @"2", @"B", nil];
    
    NSInteger count = [myDictionary count];
    id __unsafe_unretained objects[count];
    id __unsafe_unretained keys[count];
    [myDictionary getObjects:objects andKeys:keys];
    
    for (int i = 0; i < count; i++) {
      id obj = objects[i];
      id key = keys[i];
      NSLog(@"%@ -> %@", obj, key);
    }
    
    0 讨论(0)
  • 2021-02-15 17:27

    Here's the correct way to use this method:

    NSDictionary *myDictionary = [NSDictionary dictionaryWithObjectsAndKeys:@"1", @"A", @"2", @"B", nil];
    
    NSInteger count = [myDictionary count];
    id objects[count];
    id keys[count];
    [myDictionary getObjects:objects andKeys:keys];
    
    for (int i = 0; i < count; i++) {
      id obj = objects[i];
      id key = keys[i];
      NSLog(@"%@ -> %@", obj, key);
    }
    
    0 讨论(0)
提交回复
热议问题