2013-12-21 94 views
0

我在我的核心数据实体中有一个Date属性。我使用rest套件将json记录存储到核心数据中。我能够检索除日期以外的所有数据。如果我将日期更改为字符串,则可以以字符串格式获取日期。我使用mogenerator为Core Dat a实体生成类。请告知如何检索休息包中的日期字段。Restkit核心数据不检索NSDate

// DO NOT EDIT. This file is machine-generated and constantly overwritten. 
// Make changes to MarketData.m instead. 

#import "_MarketData.h" 

const struct MarketDataAttributes MarketDataAttributes = { 
    .buyPrice = @"buyPrice", 
    .sellPrice = @"sellPrice", 
    .timeStamp = @"timeStamp", 
}; 

const struct MarketDataRelationships MarketDataRelationships = { 
}; 

const struct MarketDataFetchedProperties MarketDataFetchedProperties = { 
}; 

@implementation MarketDataID 
@end 

@implementation _MarketData 

+ (id)insertInManagedObjectContext:(NSManagedObjectContext*)moc_ { 
    NSParameterAssert(moc_); 
    return [NSEntityDescription insertNewObjectForEntityForName:@"MarketData" inManagedObjectContext:moc_]; 
} 

+ (NSString*)entityName { 
    return @"MarketData"; 
} 

+ (NSEntityDescription*)entityInManagedObjectContext:(NSManagedObjectContext*)moc_ { 
    NSParameterAssert(moc_); 
    return [NSEntityDescription entityForName:@"MarketData" inManagedObjectContext:moc_]; 
} 

- (MarketDataID*)objectID { 
    return (MarketDataID*)[super objectID]; 
} 

+ (NSSet*)keyPathsForValuesAffectingValueForKey:(NSString*)key { 
    NSSet *keyPaths = [super keyPathsForValuesAffectingValueForKey:key]; 

    if ([key isEqualToString:@"buyPriceValue"]) { 
     NSSet *affectingKey = [NSSet setWithObject:@"buyPrice"]; 
     keyPaths = [keyPaths setByAddingObjectsFromSet:affectingKey]; 
     return keyPaths; 
    } 
    if ([key isEqualToString:@"sellPriceValue"]) { 
     NSSet *affectingKey = [NSSet setWithObject:@"sellPrice"]; 
     keyPaths = [keyPaths setByAddingObjectsFromSet:affectingKey]; 
     return keyPaths; 
    } 

    return keyPaths; 
} 




@dynamic buyPrice; 



- (double)buyPriceValue { 
    NSNumber *result = [self buyPrice]; 
    return [result doubleValue]; 
} 

- (void)setBuyPriceValue:(double)value_ { 
    [self setBuyPrice:[NSNumber numberWithDouble:value_]]; 
} 

- (double)primitiveBuyPriceValue { 
    NSNumber *result = [self primitiveBuyPrice]; 
    return [result doubleValue]; 
} 

- (void)setPrimitiveBuyPriceValue:(double)value_ { 
    [self setPrimitiveBuyPrice:[NSNumber numberWithDouble:value_]]; 
} 





@dynamic sellPrice; 



- (double)sellPriceValue { 
    NSNumber *result = [self sellPrice]; 
    return [result doubleValue]; 
} 

- (void)setSellPriceValue:(double)value_ { 
    [self setSellPrice:[NSNumber numberWithDouble:value_]]; 
} 

- (double)primitiveSellPriceValue { 
    NSNumber *result = [self primitiveSellPrice]; 
    return [result doubleValue]; 
} 

- (void)setPrimitiveSellPriceValue:(double)value_ { 
    [self setPrimitiveSellPrice:[NSNumber numberWithDouble:value_]]; 
} 





@dynamic timeStamp; 











@end 

============

// DO NOT EDIT. This file is machine-generated and constantly overwritten. 
// Make changes to MarketData.h instead. 

#import <CoreData/CoreData.h> 


extern const struct MarketDataAttributes { 
    __unsafe_unretained NSString *buyPrice; 
    __unsafe_unretained NSString *sellPrice; 
    __unsafe_unretained NSString *timeStamp; 
} MarketDataAttributes; 

extern const struct MarketDataRelationships { 
} MarketDataRelationships; 

extern const struct MarketDataFetchedProperties { 
} MarketDataFetchedProperties; 






@interface MarketDataID : NSManagedObjectID {} 
@end 

@interface _MarketData : NSManagedObject {} 
+ (id)insertInManagedObjectContext:(NSManagedObjectContext*)moc_; 
+ (NSString*)entityName; 
+ (NSEntityDescription*)entityInManagedObjectContext:(NSManagedObjectContext*)moc_; 
- (MarketDataID*)objectID; 





@property (nonatomic, strong) NSNumber* buyPrice; 



@property double buyPriceValue; 
- (double)buyPriceValue; 
- (void)setBuyPriceValue:(double)value_; 

//- (BOOL)validateBuyPrice:(id*)value_ error:(NSError**)error_; 





@property (nonatomic, strong) NSNumber* sellPrice; 



@property double sellPriceValue; 
- (double)sellPriceValue; 
- (void)setSellPriceValue:(double)value_; 

//- (BOOL)validateSellPrice:(id*)value_ error:(NSError**)error_; 





@property (nonatomic, strong) NSDate* timeStamp; 



//- (BOOL)validateTimeStamp:(id*)value_ error:(NSError**)error_; 






@end 

@interface _MarketData (CoreDataGeneratedAccessors) 

@end 

@interface _MarketData (CoreDataGeneratedPrimitiveAccessors) 


- (NSNumber*)primitiveBuyPrice; 
- (void)setPrimitiveBuyPrice:(NSNumber*)value; 

- (double)primitiveBuyPriceValue; 
- (void)setPrimitiveBuyPriceValue:(double)value_; 




- (NSNumber*)primitiveSellPrice; 
- (void)setPrimitiveSellPrice:(NSNumber*)value; 

- (double)primitiveSellPriceValue; 
- (void)setPrimitiveSellPriceValue:(double)value_; 




- (NSDate*)primitiveTimeStamp; 
- (void)setPrimitiveTimeStamp:(NSDate*)value; 




@end 

JSON

{ 
    "average_price": "655.66742", 
    "highest_price": "697.86887", 
    "lowest_price": "610.0", 
    "objects": 
    [ 
     { 
      "buy_price": "650.0", 
      "currency": 
      { 
       "name": "USD" 
      }, 
      "exchange": 
      { 
       "code": "mtgox" 
      }, 
      "sell_price": "653.99999", 
      "timestamp": "1387654576491207" 
     }, 
     { 
      "buy_price": "657.0", 
      "currency": 
      { 
       "name": "USD" 
      }, 
      "exchange": 
      { 
       "code": "mtgox" 
      }, 
      "sell_price": "657.48999", 
      "timestamp": "1387654280966480" 
     } 
    ] 
} 

映射

NSManagedObjectModel *managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil]; 
RKManagedObjectStore *managedObjectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:managedObjectModel]; 
NSError *error = nil; 
BOOL success = RKEnsureDirectoryExistsAtPath(RKApplicationDataDirectory(), &error); 
if (! success) { 
    RKLogError(@"Failed to create Application Data Directory at path '%@': %@", RKApplicationDataDirectory(), error); 
} 
NSString *path = [RKApplicationDataDirectory() stringByAppendingPathComponent:@"MarketData.sqlite"]; 
NSPersistentStore *persistentStore = [managedObjectStore addSQLitePersistentStoreAtPath:path fromSeedDatabaseAtPath:nil withConfiguration:nil options:nil error:&error]; 
if (! persistentStore) { 
    RKLogError(@"Failed adding persistent store at path '%@': %@", path, error); 
} 
[managedObjectStore createManagedObjectContexts]; 

RKEntityMapping *marketDataMapping = [RKEntityMapping mappingForEntityForName:@"MarketData" inManagedObjectStore:managedObjectStore]; 
[marketDataMapping addAttributeMappingsFromDictionary:@{@"buy_price" :@"buyPrice", 
                 @"sell_price" : @"sellPrice", 
                 @"timestamp" : @"timeStamp"}]; 


NSIndexSet *statusCodes = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful); // Anything in 2xx 
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:marketDataMapping method:RKRequestMethodAny pathPattern:nil keyPath:@"objects" statusCodes:statusCodes]; 

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://XXXXXXXXXXXXXXXX/market_data/?interval=50m"]]; 


RKManagedObjectRequestOperation *operation = [[RKManagedObjectRequestOperation alloc] initWithRequest:request responseDescriptors:@[responseDescriptor]]; 
operation.managedObjectContext = managedObjectStore.mainQueueManagedObjectContext; 
operation.managedObjectCache = managedObjectStore.managedObjectCache; 
[operation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *result) { 
    MarketData *article = [result firstObject]; 
    NSLog(@"Mapped the article: %@", article); 
    //NSLog(@"Mapped the category: %@", [article.categories anyObject]); 
} failure:^(RKObjectRequestOperation *operation, NSError *error) { 
    NSLog(@"Failed with error: %@", [error localizedDescription]); 
}]; 
NSOperationQueue *operationQueue = [NSOperationQueue new]; 
[operationQueue addOperation:operation]; 
+0

发布您的代码在这里 – Sport

+0

您需要显示的映射和源JSON。 RestKit可以转换为'NSDate',但您可能需要提供一个自定义转换器... – Wain

回答