我正在尝试实现简单的核心数据类来存储和检索从服务器下载的不同语言字符串(数组)。到目前为止,我已经在 App Delegate 中创建了创建的 Core Data 模板,使用一种实体“MyArray”和一种属性“语言”创建了数据模型,并编写了以下示例代码来存储和检索这些数组。现在两个字符串数组都存储在同一个属性“语言”中,我如何检索中文数组?
// Fetch arrays code
NSManagedObjectContext *context = [self managedObjectContext];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"MyArray" inManagedObjectContext:context];
[request setEntity:entity];
NSArray *results = [context executeFetchRequest:request error:nil];
for (NSManagedObject *object in results) {
NSArray *a = [object valueForKey:@"language"];
// Use array
}
-(void)storeEnglishStrings {
NSArray *array = [[NSArray alloc] initWithObjects:@"str1",@"str2", @"str3", @"str4", @"str5", nil];
NSManagedObject *object = [NSEntityDescription insertNewObjectForEntityForName:[entity name] inManagedObjectContext:context];
[object setValue:array forKey:@"language"];
NSError *error = nil;
if (![context save:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
}
-(void)storeChineseStrings {
NSArray *array = [[NSArray alloc] initWithObjects:@"串1",@"串2", @"串3", @"串4", @"串5", nil];
NSManagedObject *object = [NSEntityDescription insertNewObjectForEntityForName:[entity name] inManagedObjectContext:context];
[object setValue:array forKey:@"language"];
NSError *error = nil;
if (![context save:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
}