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

发布于 2024-12-25 03:20:45 字数 1070 浏览 3 评论 0原文

您好,我正在努力弄清楚如何循环下面的数组并将每一行添加到我的核心数据实体中,

任何帮助将不胜感激,

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

我不知道在这里该怎么做。

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

}

这是我将导入的属性的日志,

 NSLog(@"%@",importArray);

由于 csv 包含列名称,因此在命令中提供

(

    CONTACTNAME,
    PHONE,
    COMPANYNAME,
    NOTES
),

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

(

    CONTACTNAME,
    PHONE,
    COMPANYNAME,
    NOTES
),

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

池予 2025-01-01 03:20:45

如果你只有 4 个对象,那么不必费心循环,你可以简单地 -

如果你有一个 Customers 子类,你可以使用:

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

否则你将需要使用

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

BUT

如果你有很多属性CSV,您可以在实体中设置其他键数组,并且 -

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

有时更好

处理此问题的更好方法,尤其是如果您有很多属性,则可以使用 -

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

编辑
要对实体进行子类化:

  1. 在模型编辑器中选择实体。
  2. 在 Xcode 菜单中选择编辑器 ->创建 NSManagedObject 子类。
  3. 当您想要引用新子类时,导入或@class您的新子类。

顺便说一句

  1. 对您的实体进行子类化 - 这将使您的生活更轻松,并带来更好的性能。
  2. 您的实体名称应为单数“客户”,因为它仅拥有 1 个客户。

If all you have is 4 objects, don't bother looping you can simply -

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 -

     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.
  3. import or @class your new subclass when you want to refer it.

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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文