Take the 2-minute tour ×

I have read a few posts and found a method. I adapted it to my app, however at [self saveContext]; I get a bad instruction error.

Please let me know why, and if this method makes sense. Also I am using \r because in the csv each item is in its own row.

Thank you in advance

    NSString *paths = [[NSBundle mainBundle] resourcePath];
    NSString *bundlePath = [paths stringByAppendingPathComponent:@"coredatainfo.csv"];
    NSString *dataFile = [[NSString alloc] initWithContentsOfFile:bundlePath];
    NSArray *dataRows = [dataFile componentsSeparatedByString:@"\r"];
    [dataFile release];
    FirstCDitem *myItem;

    for (int i = 0 ; i < [dataRows count] ; i++)
        NSArray *dataElements = [[dataRows objectAtIndex:i] componentsSeparatedByString:@","];
        NSLog(@"Added: %d %@",i,dataElements);
        myItem = (FirstCDitem *)[NSEntityDescription insertNewObjectForEntityForName:@"FirstCDitem" inManagedObjectContext:[self managedObjectContext]];
        [FirstCDitem setTitle:[dataElements objectAtIndex:i]];
        [self saveContext];
share|improve this question
Did my answer help? –  Martin R Dec 19 '12 at 18:05

1 Answer 1

up vote 1 down vote accepted

There are two problems in this code line:

[FirstCDitem setTitle:[dataElements objectAtIndex:i]];
  • i is the current row number, so it probably makes no sense to use it as index to the dataElements array. Perhaps you want the first element in the row?
  • setTitle must sent to the instance myItem, not to the class FirstCDItem.

So the line should look like this:

[myItem setTitle:[dataElements objectAtIndex:0]];
share|improve this answer
thank you it worked –  William Falcon Dec 20 '12 at 22:45

Your Answer


