2011-06-26 78 views
2

我有一個fetch,它返回一個數組,其中包含核心數據對象的屬性字典。從NSDictionary返回NSArray

這是我剛纔的問題:Create Array From Attribute of NSObject From NSFetchResultsController

這是獲取:

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

回答

5

調用valueForKey對從提取請求返回的數組對象。這將在每個對象上調用valueForKey並返回所有結果值的數組。

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

太棒了。我愛KVC,並且每天都會學習新的東西。 – RyanR

+0

非常感謝Anurag。但由於某些原因,數據仍未加載我的日曆視圖。這是一張顯示2個數組的圖片,最下面的數據加載數據並且提供示例應用程序。最重要的是我的,並沒有加載數據。是否因爲我的時間戳需要在000000小時和時間? http://www.box.net/shared/static/s27n3hmab6r11n633pmo.png – Jon

+0

其實,我剛剛發佈了這個數組將分開的上面的方法。也許有什麼我可以改變的,以便時間0000000與我時間戳中的時間沒有什麼不同? – Jon

2

的快速計數方法:

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

Block enumerator方法:

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

無論哪種方式,'數據'只包含一個NSDate實例數組。

+0

你能檢查我發佈的其他答案中的評論,看看你是否知道發生了什麼?謝謝。 – Jon

+0

@Jon,不知道你的意思,我沒有看到你的其他問題或這個問題的其他地方有任何新的評論。你能澄清嗎? – RyanR

+0

關於Anurag提供的答案,我們正在討論一個我們無法解決的問題。 – Jon

0

爲了得到你想要陣列的類型,你可以做這樣的事情:

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

希望這將幫助你!

〜Manoj