Xcode - 创建 csv/电子表格文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12659654/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 15:49:58  来源:igfitidea点击:

Xcode - Create csv/spreadsheet file

iphoneobjective-ciosxcodearrays

提问by MCKapur

I have three arrays. The first array contains strings, same as the second, however the third one contains NSNumber's. Anyways, back on topic... how can I create a csv (which is an excel/numbers) file like a spreadsheet to contain this data. Is it possible?

我有三个数组。第一个数组包含字符串,与第二个相同,但第三个包含NSNumber's。无论如何,回到主题......我如何创建一个 csv(这是一个 excel/数字)文件,如电子表格来包含这些数据。是否可以?

These are my three arrays:

这是我的三个数组:

locationArray - contains many strings dateArray - contains many strings milesArray - contains many NSNumber's

locationArray -包含许多字符串dateArray -包含许多字符串milesArray -包含了很多NSNumber

But each array contains the same amount of objects so (for example in a table view) you can piece the data together in an NSDictionary. It is like a chain.

但是每个数组都包含相同数量的对象,因此(例如在表格视图中)您可以将数据拼凑在一个NSDictionary. 它就像一个链条。

So then it would look like this:

那么它看起来像这样:

NSDictionary *firstFlight = [NSDictionary dictionaryWithObjectsAndKeys:[locationArray objectAtIndex: 0], @"FlightName", [dateArray objectAtIndex: 0], @"FlightDate", [milesArray objectAtIndex: 0], @"FlightMiles", nil];

And that dictionary would contain a flight (the functionality of this test app is to log flights). So if I enumerated this using a forloop and replaced objectAtIndex:with the looped number I could get many dictionaries of the flights.

该字典将包含航班(此测试应用程序的功能是记录航班)。因此,如果我使用for循环枚举它并替换objectAtIndex:为循环数字,我可以获得许多航班的字典。

Now that you understand how my data structure works. How can I create an excel/numbers/spreadsheet CSV file to look like this for example (just a screencapture from what it could look like):

现在您了解了我的数据结构是如何工作的。我如何创建一个 excel/数字/电子表格 CSV 文件,例如看起来像这样(只是它可能看起来的屏幕截图):

(MILES DATA IS JUST MADE UP)

(英里数据只是编造的)

enter image description here

在此处输入图片说明

回答by Valeriy Van


NSArray *firstArray, *secondArray, *thirdArray;

NSMutableString *csv = [NSMutableString stringWithString:@"Name,Date,Miles"];

NSUInteger count = [firstArray count];
// provided all arrays are of the same length
for (NSUInteger i=0; i<count; i++ ) {
    [csv appendFormat:@"\n\"%@\",%@,\"%d\"",
        [firstArray objectAtIndex:i],
        [secondArray objectAtIndex:i],
        [[thirdArray objectAtIndex:i] integerValue]
     ];
    // instead of integerValue may be used intValue or other, it depends how array was created
}

NSString *yourFileName = @"your filename";
NSError *error;
BOOL res = [csv writeToFile:yourFileName atomically:YES encoding:NSUTF8StringEncoding error:&error];

if (!res) {
    NSLog(@"Error %@ while writing to file %@", [error localizedDescription], yourFileName );
}