xcode 如何将核心数据导出到 CSV
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10572172/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to export Core Data to CSV
提问by Desmond
I will like to use CHCSVParser
to export my Core data to CSV. I know how to get all the value from entity, but I don't know how to write to CSV.
我想用来CHCSVParser
将我的核心数据导出到 CSV。我知道如何从实体中获取所有值,但我不知道如何写入 CSV。
Can anybody teach me how to write to CSV with CHCSVParser
?
有人可以教我如何用 CSV 写入CHCSVParser
吗?
// 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);
}
回答by Ryan Poolos
A CHCSVWriter
has several methods for constructing CSV files:
ACHCSVWriter
有几种构造CSV文件的方法:
-writeField:
accepts an object and writes its -description (after being properly escaped) out to the CSV file. It will also write field seperator (,) if necessary. You may pass an empty string (@"") or nil to write an empty field.
-writeField:
接受一个对象并将其 -description(在正确转义后)写入 CSV 文件。如有必要,它还将写入字段分隔符 (,)。您可以传递一个空字符串 (@"") 或 nil 来写入一个空字段。
-writeFields:
accepts a comma-delimited and nil-terminated list of objects and sends each one to -writeField:.
-writeFields:
接受以逗号分隔且以 nil 结尾的对象列表,并将每个对象发送到 -writeField:。
-writeLine
is used to terminate the current CSV line. If you do not invoke -writeLine, then all of your CSV fields will be on a single line.
-writeLine
用于终止当前的 CSV 行。如果不调用 -writeLine,则所有 CSV 字段都将位于一行中。
-writeLineOfFields:
accepts a comma-delimited and nil-terminated list of objects, sends each one to -writeField:, and then invokes -writeLine.
-writeLineOfFields:
接受以逗号分隔且以 nil 结尾的对象列表,将每个对象发送到 -writeField:,然后调用 -writeLine。
-writeLineWithFields:
accepts an array of objects, sends each one to -writeField:, and then invokes -writeLine.
-writeLineWithFields:
接受一组对象,将每个对象发送到 -writeField:,然后调用 -writeLine。
-writeCommentLine:
accepts a string and writes it out to the file as a CSV-style comment.
-writeCommentLine:
接受一个字符串并将其作为 CSV 样式的注释写入文件。
In addition to writing to a file, CHCSVWriter
can be initialized for writing directly to an NSString
.
除了写入文件之外,CHCSVWriter
还可以初始化为直接写入NSString
.
Something Like this should work for you.
像这样的东西应该适合你。
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);
回答by Mic Fok
The answer above seems to be deprecated, the author replaced the method with another it seem. This worked for me, hope it helps:
上面的答案似乎已被弃用,作者似乎用另一种方法替换了该方法。这对我有用,希望它有帮助:
NSOutputStream *stream = [[NSOutputStream alloc] initToMemory];
CHCSVWriter *writer = [[CHCSVWriter alloc] initWithOutputStream:stream encoding:NSUTF8StringEncoding delimiter:','];
for (Type *instance in fetchedResults) {
[writer writeLineOfFields:@[instance.propertyA, instance.B]];
}
[writer closeStream];
NSData *buffer = [stream propertyForKey:NSStreamDataWrittenToMemoryStreamKey];
NSString *output = [[NSString alloc] initWithData:buffer encoding:NSUTF8StringEncoding];