加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

iphone – 如何将Core Data导出为CSV

发布时间:2020-12-15 01:45:54 所属栏目:百科 来源:网络整理
导读:我想使用CHCSVParser将我的Core数据导出为CSV.我知道如何从实体获取所有价值,但我不知道如何写入CSV. 谁能教我如何使用CHCSVParser写入CSV? // Test listing all Infos from the storeNSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];NSEnti
我想使用CHCSVParser将我的Core数据导出为CSV.我知道如何从实体获取所有价值,但我不知道如何写入CSV.

谁能教我如何使用CHCSVParser写入CSV?

// Test listing all Infos from the store
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription 
                               entityForName:@"NoteLog" inManagedObjectContext:context];
[fetchRequest setEntity:entity];

NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];
for (NoteLog *noteInfo in fetchedObjects) {

    NSLog(@"Name: %@",noteInfo.city );
    NSLog(@"Name: %@",noteInfo.country);
    NSLog(@"Name: %@",noteInfo.datetime);
    NSLog(@"Name: %@",noteInfo.notelatitude);
    NSLog(@"Name: %@",noteInfo.notelongtitude);
    NSLog(@"Name: %@",noteInfo.state);
    NSLog(@"Name: %@",noteInfo.text);        
}

解决方法

CHCSVWriter有几种构造CSV文件的方法:

-writeField:接受一个对象并将其-description(在被正确转义后)写入CSV文件.如有必要,它还会写字段分隔符(,).您可以传递一个空字符串(@“”)或nil来写一个空字段.

-writeFields:接受以逗号分隔且以nil结尾的对象列表,并将每个对象发送到-writeField:.

-writeLine用于终止当前的CSV行.如果不调用-writeLine,则所有CSV字段都将在一行中.

-writeLineOfFields:接受以逗号分隔且以nil结尾的对象列表,将每个对象发送到-writeField:,然后调用-writeLine.

-writeLineWithFields:接受一个对象数组,然后调用-writeLine.

-writeCommentLine:接受一个字符串并将其作为CSV样式的注释写入文件.

除了写入文件外,还可以初始化CHCSVWriter以直接写入NSString.

像这样的东西对你有用.

CHCSVWriter *writer = [[CHCSVWriter alloc] initForWritingToString];

for (NoteLog *noteInfo in fetchedObjects) {

    [writer writeLineOfFields:noteInfo.city,noteInfo.country,noteInfo.datetime,noteInfo.notelatitude,noteInfo.notelongtitude,noteInfo.state,noteInfo.text,nil];     
}  

NSLog(@"My CSV File: %@",writer.stringValue);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读