我使用的是API,它们的类似乎不符合Core数据。
我试图存储AftershipCheckpoint和AftershipTracking的类
用于.h的AftershipTracking文件如下
@interface AftershipTracking : NSObject
@property (nonatomic, strong) NSString* createTime;
@property (nonatomic, strong) NSString* updateTime;
@property (nonatomic, strong) NSString* identifier;
@property (nonatomic, strong) NSString* trackingNumber;
@property (nonatomic, strong) NSString* trackingPostalCode;
@property (nonatomic, strong) NSString* trackingShipDate;
@property (nonatomic, strong) NSString* trackingAccountNumber;
@property (nonatomic, strong) NSString* slug;
@property (nonatomic) bool isActive;
@property (nonatomic, strong) NSDictionary* customFields;
@property (nonatomic, strong) NSString* customName;
@property (nonatomic, strong) NSNumber* deliveryTime;
@property (nonatomic, strong) NSString* destionationCountryCode;
@property (nonatomic, strong) NSArray* emails;
@property (nonatomic, strong) NSString* expectedDeliveryDate;
@property (nonatomic, strong) NSString* orderId;
@property (nonatomic, strong) NSString* orderIdPath;
@property (nonatomic, strong) NSString* originCountryCode;
@property (nonatomic, strong) NSString* uniqueToken;
@property (nonatomic, strong) NSNumber* shipmentPackageCount;
@property (nonatomic, strong) NSString* shipmentType;
@property (nonatomic, strong) NSNumber* shipmentWeight;
@property (nonatomic, strong) NSString* shipmentWeightUnit;
@property (nonatomic, strong) NSString* signedBy;
@property (nonatomic, strong) NSArray* smses;
@property (nonatomic, strong) NSString* source;
@property (nonatomic, strong) NSString* tag;
@property (nonatomic, strong) NSString* title;
@property (nonatomic, strong) NSNumber* trackedCount;
@property (nonatomic, strong) NSArray* checkpoints;
- (NSString *)description;它包含一个AftershipCheckpoint数组,所以我想我也需要配置这个类。
@interface AftershipCheckpoint : NSObject
@property (nonatomic, strong) NSDate* createTime;
@property (nonatomic, strong) NSString* slug;
@property (nonatomic, strong) NSString* checkpointTime;
@property (nonatomic, strong) NSString* city;
@property (nonatomic, strong) NSString* state;
@property (nonatomic, strong) NSString* countryCode;
@property (nonatomic, strong) NSString* countryName;
@property (nonatomic, strong) NSString* message;
@property (nonatomic, strong) NSString* tag;
@property (nonatomic, strong) NSString* zip;
- (NSString *)description;我的应用程序有一个AftershipTracking数组,如何使用数据存储持久数据数组,或者是否有更好的选择?
发布于 2015-01-03 02:39:28
为了从您的模型中删除一个跟踪,它将如下所示。
- (void)remove:(AftershipTracking *)package
{
[_packages removeObject:package];
[_delivered removeObject:package];
[_inProgress removeObject:package];
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = [appDelegate managedObjectContext];
NSEntityDescription *entityDesc = [NSEntityDescription entityForName:@"Tracking" inManagedObjectContext:context];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entityDesc];
NSPredicate *pred =[NSPredicate predicateWithFormat:@"(trackingNumber = %@)", package.trackingNumber];
[request setPredicate:pred];
NSManagedObject *match = nil;
NSError *error;
NSArray *objects = [context executeFetchRequest:request error:&error];
match = objects.firstObject;
[context deleteObject:match];
[self sort];
[context save:&error];
}发布于 2014-12-23 23:34:42
那里的所有东西似乎都准备好了使用NSCoding来实现持久性。首先,根据可以将这些数组保存到NSUserDefaults中的大小而定,或者您可以启动一个Core并尝试使用plist进行持久化处理。通常,首先要确保您的逻辑是正确的,方法是使用人类可读的formate (在iOS的情况下是plist),然后一旦得到了,就将存储切换到使用SQLite,但是只有在有数千个对象要持久化的情况下才真正需要这样做。
使用NSUserDefaults,只要AftershipTracking符合NSCoding,数组就会保存在AftershipCheckpoint的列表中。
发布于 2014-12-30 16:56:48
在AftershipTracking中,
isActive更改为NSNumber。作为一个NSNumber,它可以保存在CoreData中。checkpoints是AftershipCheckpoint对象的数组。这可以设置为模型中的多个关系,这将将类型更改为NSSet。当您从NSSet中检索出CoreData时,可以将其转换为带有[checkpoints allObjects]的数组。NSArray和NSDictionary的对象需要是可转换对象。在AftershipCheckpoint中,
NSString和NSDate。这两个类都需要是NSManagedObjectModel的子类。通过常规的核心数据建模选项(一定要导入核心数据框架)创建它们可能是个好主意。查看核心数据上的这是Ray Wenderlich教程。
https://stackoverflow.com/questions/27629438
复制相似问题