【问题标题】:Core Data - How to Insert an Array核心数据 - 如何插入数组
【发布时间】:2012-02-02 22:47:38
【问题描述】:

我想知道如何使用 Core Data 保存几个字符串对象,所有这些对象都存储在一个数组中。

我了解如何存储单个字符串,但是有什么方便的方法/我可以存储数组对象本身而不是遍历数组并分别存储每个字符串项吗?

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

基本上,我可以让 setValue 成为一个数组而不是 Alice 吗?

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

感谢您的帮助

【问题讨论】:

    标签: iphone ios xcode ios4 core-data


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

      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];
      

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

      【讨论】:

        【解决方案3】:

        你在哪里用你的数组调用过这个方法

        -(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;
        }
        

        【讨论】:

          猜你喜欢
          • 2015-07-20
          • 2017-05-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-05-02
          • 2018-05-21
          • 1970-01-01
          相关资源
          最近更新 更多