Return NSArray from NSDictionary

后端 未结 3 1438
太阳男子
太阳男子 2021-01-21 17:58

I have a fetch that returns an array with dictionary in it of an attribute of a core data object.

Here is my previous question: Create Array From Attribute of NSObject F

相关标签:
3条回答
  • 2021-01-21 18:12

    Call valueForKey on the array object returned from the fetch request. That will in-turn call valueForKey on each object and return an array of all resulting values.

    NSArray *timestamps = [objects valueForKey:@"timeStamp"];
    
    0 讨论(0)
  • 2021-01-21 18:12

    The Fast Enumeration method:

    NSMutableArray *data = [[NSMutableArray alloc] initWithCapacity:array.count];
    for (NSDictionary *d in objects) {
      [data addObject:[d objectForKey:@"timeStamp"]];
    }
    

    The Block enumerator method:

    NSMutableArray *data = [[NSMutableArray alloc] initWithCapacity:array.count];
    [objects enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
      [data addObject:[obj objectForKey:@"timeStamp"]];
    }];
    

    Either way, 'data' contains just an array of NSDate instances.

    0 讨论(0)
  • 2021-01-21 18:16

    To get the type of array you want, you can do something like this:

    NSMutableArray *array = [[NSMutableArray alloc] init];
    for (int n = 0; n < [data count]; n++) // data array 
     {  
        NSMutableArray *array = [[NSMutableArray alloc] init];
        array = [NSMutableArray arrayWithObjects:[[data objectAtIndex:n] valueForKey:@"timeStamp"] ,nil];
       if ([array count] != 0) {
        [newArray addObject:[array objectAtIndex:0]];
        }
      }
     [array release];
    

    hope this will help you!!

    ~Manoj

    0 讨论(0)
提交回复
热议问题