需要帮助才能将Core Data导出为CSV工作

时间:2014-10-15 23:36:43

标签: ios objective-c csv core-data

我不得不说我是Cocoa编程的初学者,虽然我在答案中找到了“食谱”的方法:Exporting the core data into csv via mail composer using CHCSVParser(by Dave DeLong) 我仍然无法使它工作。我知道的洞太多了。 我正在尝试从数据模型中的实体形成CSV文件,为用户显示该文件,然后通过电子邮件发送该文件。 无论如何,当我看到上面链接的答案中的细节时,我被卡住了。我应该把那部分代码放在哪里?在ViewControler中为用户显示文件?那么究竟在viewDidLoad部分或其他地方? 任何提示,甚至是关于整个程序的更详细解释的链接都将是非常受欢迎的。

提前感谢!

Xcode抛出(作为注释)错误的代码部分如下:

#import "SendingViewController.h"
#import "DOAppDelegate.h"
#import "Record.h"
#import "CHCSVParser.h"

@interface SendingViewController (){

}
@end

@implementation SendingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    NSManagedObjectContext *moc = [self managedObjectContext]; 
//Error: No visible     @interface for 'sending View Controller' declares the selector 'managedObjectContext'
    NSEntityDescription *entityDescription = [NSEntityDescription
                                          entityForName:@"Record"   inManagedObjectContext:moc];
    NSFetchRequest *request = [[NSFetchRequest alloc] init];


    request.predicate = [NSPredicate predicateWithFormat:@"rs_Record.name = %@", self.projectObject.name];
//Error: Property 'projectObject' not found on object of type 'sendingViewController *'


[request setEntity:entityDescription];
request.resultType = NSDictionaryResultType;

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"Observer" ascending:YES];
[request setSortDescriptors:@[sortDescriptor]];

NSError *error;

NSArray *fetchedObjects = [moc executeFetchRequest:request error:&error];

//creating a csv CHCSVWriter
NSOutputStream *output = [NSOutputStream outputStreamToMemory];
CHCSVWriter *writer = [[CHCSVWriter alloc] initWithOutputStream:output encoding:NSUTF8StringEncoding delimiter:','];
//wrting header name for csv file
[writer writeField:@"Observer"];
[writer writeField:@"Time"];
[writer writeField:@"Lat"];
[writer writeField:@"Long"];
[writer writeField:@"Distance"];
[writer writeField:@"Doe"];
[writer writeField:@"Fawn"];
[writer writeField:@"Buck"];
[writer finishLine];



for (NSManagedObject *object in fetchedObjects)
{
    //getting the data from core data


    int doe_no = [[object valueForKey:@"doeNum"] intValue];
    int buck_no = [[object valueForKey:@"buckNum"] intValue];
    int fawn_no = [[object valueForKey:@"fawnNum"] intValue];
    int distance = [[object valueForKey:@"distance"] intValue];
    float latValue=[[object valueForKey:@"latitude"] floatValue];
    float lonValue=[[object valueForKey:@"longitude"] floatValue];

    NSString *obser=[object valueForKey:@"observer"];
    NSDate *time= [object valueForKey:@"time"];

    //writing that data to writer for csv file

    [writer writeField:[NSString stringWithFormat:@"%@",obser]];
    [writer writeField:[NSString stringWithFormat:@"%@",time]];
    [writer writeField:[NSString stringWithFormat:@"%f",latValue]];
    [writer writeField:[NSString stringWithFormat:@"%f",lonValue]];
    [writer writeField:[NSString stringWithFormat:@"%i",distance]];
    [writer writeField:[NSString stringWithFormat:@"%i",doe_no]];
    [writer writeField:[NSString stringWithFormat:@"%i",fawn_no]];
    [writer writeField:[NSString stringWithFormat:@"%i",buck_no]];


    [writer finishLine]; //finishing the writing of first row

}

[writer closeStream];


NSData *buffer = [output propertyForKey:NSStreamDataWrittenToMemoryStreamKey];

NSString *string = [[NSString alloc] initWithData:buffer encoding:NSUTF8StringEncoding];
//NSLog(@"string = %@",string);

}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}




- (IBAction)showEmail:(id)sender {

2 个答案:

答案 0 :(得分:0)

我不知道你究竟要求什么,但如果你不知道何时导出到CSV,最简单的地方就是在发送电子邮件之前将其导出。因此,用户点击“通过电子邮件发送”按钮,调用-(void)sendEmail:(id)sender;,然后导出到csv,准备电子邮件,将csv附加到它并呈现邮件控制器。

我也看到,你正在通过搜索它的名字来寻找Record对象。您应该只传递谓词对象(或objectId)而不是它的名称属性。

- 更新谓词

您的代码:

request.predicate = [NSPredicate predicateWithFormat:@"rs_Record.name = %@", self.projectObject.name];

应改为:

request.predicate = [NSPredicate predicateWithFormat:@"rs_Record = %@", self.projectObject];

答案 1 :(得分:0)

您在代码中提到的错误非常准确。

第一个告诉你,你确实没有在视图控制器中定义一个名为“managedObjectContext”的属性/方法。您在核心数据中所做的一切都需要一个上下文,因此您需要向视图控制器提供一些内容。如果您有NSManagedObject,则可以使用其.managedObjectContext属性。

第二个告诉你,你确实没有在视图控制器中定义一个名为“projectObject”的属性。

如果你猜我猜 1.在视图控制器上创建一个名为“projectObject”的公共属性,并在呈现视图控制器之前为其赋值, 2.使用projectObject.managedObjectContext而不是[self managedObjectContext],  你的错误会消失。