核心数据 - 如何插入数组

时间:2012-01-05 02:40:40

标签: iphone ios xcode ios4 core-data

我想知道如何使用Core Data保存多个存储在数组中的字符串对象。

我理解如何存储单个字符串,但有没有方便的方法/我可以存储数组对象本身而不是迭代数组并单独存储每个字符串项目?

NSManagedObject *alice = [NSEntityDescription insertNewObjectForEntityForName:@"Student" inManagedObjectContext:context];
[alice setValue:@"Alice" forKey:@"name"];
[alice setValue:@"Computer Science" forKey:@"major"];

基本上,我可以将setValue改为数组而不是Alice吗?

(作为一个不相关的问题,如何在iphone上缓存数据,例如图像......)

感谢您的帮助

3 个答案:

答案 0 :(得分:0)

请看这篇文章:insert NSDictionary into CoreData只需将NSDictionary替换为NSArray,除了它是同一个问题。

答案 1 :(得分:0)

我不知道是否有任何预建方法。例如,您必须在单独的容器中跟踪键,或者在某处将它们定义为常量,例如在以下示例中:

static NSUInteger const kMyNameIdx = 0U;
static NSUInteger const kMyMajorIdx = 1U;
static NSString * const kMyNameKey = @"name";
static NSString * const kMyMajorKey = @"major";

/* this does no error checking on the mo or array */
/* being null. it would be better to return an    */
/* NSError from this function and check its value */
/* to handle error cases                          */

- (void) updateManagedObject:(NSManagedObject *)mo withOrderedArray:(NSArray *)array
{
    id obj;
    NSUInteger objIdx = 0U;

    /* this assumes that name and major objects in */
    /* your array are in the same order as set by  */
    /* the constants                               */

    for (obj in array) {
        switch (objIdx) { 
            case kMyNameIdx:
                [mo setValue:obj forKey:kMyNameKey];
                break;
            case kMyMajorIdx:
                [mo setValue:obj forKey:kMyMajorKey];
                break;
            default:
                break;
        }
        objIdx++;
    }
}

使用它:

NSManagedObject *alice = [NSEntityDescription insertNewObjectForEntityForName:@"Student" inManagedObjectContext:context];
NSArray *myArray = /* ... */
[self updateManagedObject:alice withOrderedArray:myArray];

您可以进行其他修改,例如对NSManagedObject实体类型Student设置category method。然后,您可以在使用Student托管对象的任何地方调用此函数。

答案 2 :(得分:0)

你曾用数组调用此方法

-(void)viewDidLoad
{
  [self insertLoginData:YOUR ARRAY NAME];
}

- (BOOL)insertLoginData:(NSMutableArray *)loginInfoArray
{
    NSError *error=nil;
    NSManagedObjectContext *context = [self managedObjectContext];
    NSManagedObject *propertyInfo = [NSEntityDescription
                                     insertNewObjectForEntityForName:@"UserLogin" 
                                     inManagedObjectContext:context];
    for(int count=0;count<[loginInfoArray count];count++)
    {
        [propertyInfo setValue:[[loginInfoArray objectAtIndex:count]objectForKey:@"UserId"] forKey:@"userName"];
        [propertyInfo setValue:[[loginInfoArray objectAtIndex:count]objectForKey:@"Password"] forKey:@"password"];  
    }

    if (![__managedObjectContext save:&error]) {
        NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
        return NO;
    }
    else  
    {
        return YES; 
    }
}

获取数据==========

-(NSMutableArray *)fetchLoginData
{
    NSFetchRequest *fetchReq = [[NSFetchRequest alloc]init];

    [fetchReq setEntity:[NSEntityDescription entityForName:@"UserLogin" inManagedObjectContext:self.managedObjectContext]];
    NSMutableArray *resultArray = [[NSMutableArray alloc]initWithArray:[self.managedObjectContext executeFetchRequest:fetchReq error:nil]];
    NSMutableArray *array=[[NSMutableArray alloc]init];
    for(UserLogin *pnt in resultArray)
    {
        //[array addObject:pnt.userName];
        [array addObject:pnt];
    }
    return array;
}