简体   繁体   English

如何将该数组加载到核心数据实体中?

[英]How do I load this array into Core Data Entity?

Hi I am struggling to figure out how to loop through the array below and add each row into my core data entity 嗨,我正在努力找出如何遍历下面的数组并将每一行添加到我的核心数据实体中

Any help would be greatly appreciated 任何帮助将不胜感激

//CREATE AN ARRAY FROM CSV DOCUMENT USING CHCSVPARSER
NSError *error;
NSString *customerCSV = [[NSBundle mainBundle] pathForResource:@"CUSTOMERS" ofType:@"csv"];
NSArray *importArray = [NSArray arrayWithContentsOfCSVFile:customerCSV encoding:NSUTF8StringEncoding error:&error];
NSLog(@"%@",importArray);

//LOOP THROUGH CREATED ARRAY AND ADD OBJECTS TO COREDATA CUSTOMER ENTITY
Invoice_MarketAppDelegate* delegate = [[UIApplication sharedApplication] delegate];
NSManagedObjectContext* managedObjectContext = delegate.managedObjectContext;
NSManagedObject* newCustomer;
newCustomer = [NSEntityDescription insertNewObjectForEntityForName:@"Customers" inManagedObjectContext:managedObjectContext];

I don't know what to do here. 我不知道该怎么办。

for () {
    NSLog(@"importing Row");

}

Here is a log of the attributes I will be importing, provided at the command 这是我将要导入的属性的日志,在命令中提供

 NSLog(@"%@",importArray);

since the csv included column names 由于csv包含列名

(

    CONTACTNAME,
    PHONE,
    COMPANYNAME,
    NOTES
),

If all you have is 4 objects, don't bother looping you can simply - 如果您只有4个对象,则无需打扰循环,您可以简单地-

If you have a sub class of Customers you can use: 如果您有客户的子类,则可以使用:

 newCustomer.contactName = [importArray objectAtIndex:0];//change it to the correct index, and correct property name
 newCustomer.phone = [importArray objectAtIndex:1];
 //....And so on

else you will need to use 否则您将需要使用

 [newCustomer objectForKey:@"contactName"] = [importArray objectAtIndex:0];

BUT

If you have many properties in your CSV you can set an other array of the keys in your entity and - 如果CSV中有许多属性,则可以在实体中设置其他键数组,并且-

     for(NSUInteger i=0;i<[importArray count];i++){
       [newCustomer objectForKey:[keysArry objectAtIndex:i]] = [importArray objectAtIndex:i];  
     }

Better sometimes 有时更好

A better way to handle this, ecpaciely if you have many properties is to use - 如果您有很多属性,那么最好地使用一种更好的方法来处理此问题-

 //1. crate a dictionary from your CSV with keys that are similar to your entity property names.
 NSDictionary *csvDictinary = []//set your dictionary.
 //2.get all the property names from your customers entity
 NSDictionary *attributes = [[NSEntityDescription
                             entityForName:@"Costumer"
                             inManagedObjectContext:self] attributesByName];
//3. set the properties to your entity
for (NSString *attr in attributes) {
    [Costumer setValue:[csvDictinary valueForKey:attr] forKey:attr];
}

EDIT To sub class your entity: 编辑子类化您的实体:

  1. select the entity in the model editor. 在模型编辑器中选择实体。
  2. in Xcode menu select Editor -> Create NSManagedObject Subclass. 在Xcode菜单中,选择编辑器->创建NSManagedObject子类。
  3. import or @class your new subclass when you want to refer it. 要引用新子类时,请导入或@class。

BTW BTW

  1. Subclass your entities - it will make your life easier and will cause better performance. 对您的实体进行子类化-将使您的生活更轻松,并会带来更好的性能。
  2. Your entity name should be -"Customer" in singular, as it hold only 1 customer. 您的实体名称应为-“ Customer”(单数),因为它仅容纳1位客户。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM