使用RestKit将JSON字符串映射到NSDate对象

时间:2013-07-23 10:31:18

标签: iphone objective-c nsdate restkit nsdateformatter

我有一个JSON,其中包含这样的日期对象。

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。那个对象管理员我将这样使用。

-(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吗?

2 个答案:

答案 0 :(得分:0)

试试这个:

[dateFormatter  setDateFormat:@"dd/M/yyyy"];

并将代码放在用格式化程序创建日期的位置。

答案 1 :(得分:0)

NSString *date = @"22/02/2014";

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd/MM/yyyy"];

NSDate *date = [dateFormatter dateFromString:date];

我认为上面的代码对你有用......