我有一個JSON
,它包含一個像這樣的日期對象。使用RestKit將JSON字符串映射到NSDate對象中
date: "22/02/2014"
現在我想將它映射到一個NSDate
對象。所以在我的映射中我添加了這個。
NSDateFormatter* dateFormatter = [NSDateFormatter new];
[dateFormatter setDateFormat:@"dd/MM/YYYY"];
kalenderMapping.dateFormatters = [NSArray arrayWithObject: dateFormatter];
當我登錄我的NSDate
對象我得到這個。
date is 3112-12-31 23:00:00 +0000
有人知道問題是什麼?
謝謝!
編輯
這是我如何使用kalenderMapping。
首先這裏是我的全部功能。
-(RKObjectManager *)mapGetKalender{
RKEntityMapping* kalenderMapping = [RKEntityMapping mappingForEntityForName:@"Kalender" inManagedObjectStore:managedObjectStore];
kalenderMapping.identificationAttributes = @[@"kal_date"] ;
[kalenderMapping addAttributeMappingsFromDictionary:@{
@"date": @"kal_date",
@"hour": @"kal_hour",
@"type": @"kal_type",
@"home": @"kal_home",
@"away": @"kal_away",
@"homeScore": @"kal_homeScore",
@"awayScore": @"kal_awayScore",
@"homeIcon": @"kal_homeIcon",
@"awayIcon": @"kal_awayIcon",
@"date_text": @"kal_date_text",
}];
NSDateFormatter* dateFormatter = [NSDateFormatter new];
[dateFormatter setDateFormat:@"dd/M/yyyy"];
kalenderMapping.dateFormatters = [NSArray arrayWithObject: dateFormatter];
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:kalenderMapping
pathPattern:nil
keyPath:@"sportief.calendar" statusCodes:[NSIndexSet indexSetWithIndex:200]];
NSArray *arrResponsDescriptor = [[NSArray alloc]initWithObjects:responseDescriptor, nil];
[objectManager addResponseDescriptorsFromArray:arrResponsDescriptor];
return objectManager;
}
這將返回一個objectManager。我將使用這樣的objectmanager。
-(void)fetchKalender{
API *api = [API new];
RKManagedObjectStore *store = [[YellowstarsDataModel sharedDataModel] objectStore];
NSManagedObjectContext *context = store.mainQueueManagedObjectContext;
RKObjectManager *objectManager = [api mapGetKalender];
NSString *request = @"/mobile/json/request/calendar/type/ipad";
[objectManager getObjectsAtPath:request parameters:nil
success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
NSError *error = nil;
BOOL success = [store.mainQueueManagedObjectContext save:&error];
if (!success) RKLogWarning(@"Failed saving managed object context: %@", error);
NSLog(@"DONE FETCHING");
}
failure:^(RKObjectRequestOperation *operation, NSError *error) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
message:[error localizedDescription]
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
NSLog(@"Hit error: %@", error);
}];
NSError *error = nil;
[context save:&error];
}
執行此函數後,我想我的所有日期都存儲爲NSDates在我的核心數據庫中沒有?
你需要解釋如何kalendarMapping值被用來映射的日期。您還需要使用「yyyy」而不是「YYYY」。 –
我編輯了我的問題。如果您需要更多信息,請詢問! – Steaphann
什麼日期格式,你實際使用的問題現在顯示了2個不同的版本... – Wain