保存したデータを CoreData から CSV ファイルにエクスポートしようとすると問題が発生します。私の最終的な目標は、情報を CSV に保存し、メールに添付することです。データをエクスポートするための私のコードは次のとおりです。
- (IBAction)exportData
{
mieleWasherAppDelegate *exportObjects = [[mieleWasherAppDelegate alloc] init];
[exportObjects managedObjectContext];
NSFetchRequest * allContacts = [[NSFetchRequest alloc] init];
[allContacts setEntity:[NSEntityDescription entityForName:@"Contacts" inManagedObjectContext: [exportObjects managedObjectContext]]];
// [allContacts setIncludesPropertyValues:YES]; //only fetch the managedObjectID
NSError * error = nil;
NSArray * entries = [[exportObjects managedObjectContext] executeFetchRequest:allContacts error:&error];
[allContacts release];
//error handling goes here
NSMutableArray *array = [[NSMutableArray alloc] init];
for (Contacts * entry in entries)
{
NSLog(@"Salutation: %@", entry.Salutation);
NSLog(@"First Name: %@", entry.FirstName);
NSLog(@"Last Name: %@", entry.LastName);
NSLog(@"Company Name: %@", entry.CompanyName);
NSLog(@"Email Address: %@", entry.EmailAddress);
NSLog(@"Phone Number: %@", entry.PhoneNumber);
[array addObject:entry];
}
NSError *saveError = nil;
[[exportObjects managedObjectContext] save:&saveError];
//more error handling here
// Export to CSV code
NSString *separator = @", ";
NSString *csv = @"";
for (NSArray *entry in entries)
{
csv = [NSString stringWithFormat:@"%@%@%@%@%@\n", csv, [entry Salutation],
separator, [entry FirstName],
separator, [entry LastName],
separator, [entry CompanyName],
separator, [entry EmailAddress],
separator, [entry PhoneNumber]];
}
//If you want to store in a file the CVS
//[csv writeToFile:pathToFile atomically:YES];
//If you want to store in a file the CVS
//[cvs writeToFile:pathToFile atomically:YES];
}
フォーマットが少し乱雑で申し訳ありませんが、次の警告が返されます (繰り返し):
NSArray は「-Salutation」に応答しない場合があります
など、すべてのフィールドを通過するまで。なぜ協力しないのか理解できないようです。何かアドバイス?