0

我在尝试将保存的数据从 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”

等等,直到它遍历所有领域。我似乎无法弄清楚为什么它不会合作。有什么建议吗?

4

1 回答 1

2

在上面复制和粘贴的代码的第二个for循环中,您声明entryNSArray对象而不是Contacts对象。

更改NSArrayContacts,警告将消失。

于 2012-02-28T15:16:55.220 回答