繁体   English   中英

从NSDictionary返回NSArray

[英]Return NSArray from NSDictionary

我有一个访存,它返回一个包含字典的数组,其中包含核心数据对象的属性。

这是我以前的问题: 从NSFetchResultsController的NSObject属性创建数组

这是提取:

NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entity];
[request setResultType:NSDictionaryResultType];
[request setReturnsDistinctResults:NO]; //set to YES if you only want unique values of the property
[request setPropertiesToFetch :[NSArray arrayWithObject:@"timeStamp"]]; //name(s) of properties you want to fetch

// Execute the fetch.
NSError *error;
NSArray *objects = [managedObjectContext executeFetchRequest:request error:&error];

当我记录NSArray data ,得到以下信息:

The content of data is(
        {
        timeStamp = "2011-06-14 21:30:03 +0000";
    },
        {
        timeStamp = "2011-06-16 21:00:18 +0000";
    },
        {
        timeStamp = "2011-06-11 21:00:18 +0000";
    },
        {
        timeStamp = "2011-06-23 19:53:35 +0000";
    },
        {
        timeStamp = "2011-06-21 19:53:35 +0000";
    }
)

我想要的是具有这种格式的数组:

[NSArray arrayWithObjects: @"2011-11-01 00:00:00 +0000", @"2011-12-01 00:00:00 +0000", nil];'

编辑:

这是我要用新数据数组替换数据数组的方法:

- (NSArray*)calendarMonthView:(TKCalendarMonthView *)monthView marksFromDate:(NSDate *)startDate toDate:(NSDate *)lastDate {    
    NSLog(@"calendarMonthView marksFromDate toDate");   
    NSLog(@"Make sure to update 'data' variable to pull from CoreData, website, User Defaults, or some other source.");
    // When testing initially you will have to update the dates in this array so they are visible at the
    // time frame you are testing the code.
    NSArray *data = [NSArray arrayWithObjects:
                     @"2011-01-01 00:00:00 +0000", @"2011-12-01 00:00:00 +0000", nil]; 


    // Initialise empty marks array, this will be populated with TRUE/FALSE in order for each day a marker should be placed on.
    NSMutableArray *marks = [NSMutableArray array];

    // Initialise calendar to current type and set the timezone to never have daylight saving
    NSCalendar *cal = [NSCalendar currentCalendar];
    [cal setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];

    // Construct DateComponents based on startDate so the iterating date can be created.
    // Its massively important to do this assigning via the NSCalendar and NSDateComponents because of daylight saving has been removed 
    // with the timezone that was set above. If you just used "startDate" directly (ie, NSDate *date = startDate;) as the first 
    // iterating date then times would go up and down based on daylight savings.
    NSDateComponents *comp = [cal components:(NSMonthCalendarUnit | NSMinuteCalendarUnit | NSYearCalendarUnit | 
                                                    NSDayCalendarUnit | NSWeekdayCalendarUnit | NSHourCalendarUnit | NSSecondCalendarUnit) 
                                          fromDate:startDate];
    NSDate *d = [cal dateFromComponents:comp];

    // Init offset components to increment days in the loop by one each time
    NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];
    [offsetComponents setDay:1];    


    // for each date between start date and end date check if they exist in the data array
    while (YES) {
        // Is the date beyond the last date? If so, exit the loop.
        // NSOrderedDescending = the left value is greater than the right
        if ([d compare:lastDate] == NSOrderedDescending) {
            break;
        }

        // If the date is in the data array, add it to the marks array, else don't
        if ([data containsObject:[d description]]) {
            [marks addObject:[NSNumber numberWithBool:YES]];
        } else {
            [marks addObject:[NSNumber numberWithBool:NO]];
        }

        // Increment day using offset components (ie, 1 day in this instance)
        d = [cal dateByAddingComponents:offsetComponents toDate:d options:0];
    }

    [offsetComponents release];

    return [NSArray arrayWithArray:marks];
}

对从提取请求返回的数组对象调用valueForKey 这将依次调用每个对象的valueForKey并返回所有结果值的数组。

NSArray *timestamps = [objects valueForKey:@"timeStamp"];

快速枚举方法:

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

块枚举器方法:

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

无论哪种方式,“数据”都只包含NSDate实例的数组。

要获得所需的数组类型,可以执行以下操作:

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];

希望这个能对您有所帮助!!

〜马诺

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM