我有一个应用程序可以创建单个事件并将它们存储在核心数据中。我需要做的是单独加载一个然后通过电子邮件导出它。下面的代码工作,除了它导出我需要它的每个事件只导出选定的索引路径。代码确实加载了相应的记录,因为NSLog(@"我的记录是:%@",currentItem);仅显示该事件的设置,但是当数据导出到电子邮件时,将发送所有事件。我需要导出事件名称的所选事件。有什么想法吗?
NSInteger index = exportevent.tag;
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0];
CDBaseItem *rawRecord = [self.fetchedResultsController objectAtIndexPath:indexPath];
CDSurveyItem *surveyItem = [CDSurveyItem castObject:rawRecord];
self.recordEditID = [rawRecord.objectID URIRepresentation];
NSManagedObjectID *objectId = [self.managedObjectContext.persistentStoreCoordinator managedObjectIDForURIRepresentation:self.recordEditID];
TSPItem *currentItem = [self.managedObjectContext objectWithID:objectId];
NSString *eventName = nil;
if (currentItem.eventname) {
eventName = currentItem.eventname;
}
else if (surveyItem.eventname) {
eventName = surveyItem.eventname;
}
[self setSelection:indexPath];
if (self.selection)
{
if (currentItem)
{
NSLog (@"My record is: %@", currentItem);
NSData *export = [CDJSONExporter exportContext:currentItem.managedObjectContext auxiliaryInfo:nil];
MFMailComposeViewController *composeVC1 = [[MFMailComposeViewController alloc] init];
composeVC1 = [[MFMailComposeViewController alloc] init];
composeVC1.mailComposeDelegate = self;
[composeVC1 setSubject:[NSString stringWithFormat:@"Settings From %@ Event", eventName]];
[composeVC1 setMessageBody:[NSString stringWithFormat:@"Here is the event settings. Simply press on the attachment and then choose Open in iPIX"] isHTML:NO];
[composeVC1 addAttachmentData:export mimeType:@"application/octet-stream" fileName:[NSString stringWithFormat:@"%@.ipix", eventName]];
[self presentViewController:composeVC1 animated:NO completion:^(void){}];
}
[self setSelection:nil];
}
答案 0 :(得分:0)
您的NSLog
可能是正确的,但您没有导出正在打印的内容。在这一行(我假设是对this project的引用):
NSData *export = [CDJSONExporter exportContext:currentItem.managedObjectContext auxiliaryInfo:nil];
您告诉CDJSONExporter
导出上下文,而不是单个对象。你得到每个对象,因为是CDJSONExporter
做的。它获取它在上下文中可以找到的所有内容,并为您提供数据对象。它不是为了做你要求它做的事。
如果要将单个对象转换为JSON,可以
Encodable
,然后使用JSONEncoder
转换为JSON。或...