简体   繁体   English

格式化NSMutableArray for .csv文件的内容

[英]Format contents of NSMutableArray for .csv file

I have a picture slideshow app which allows the user to rate 3 images. 我有一个图片幻灯片应用程序,该应用程序允许用户对3张图像进行评分。 The ratings get stored in a NSMutableArray called rated like this: 等级被存储在一个名为NSMutableArray rated如下所示:

    2014-01-06 07:10:23.040 SlideShowSurvey[50425:70b] (
    1, <-- Rating for Picture 1
    2, <-- Rating for Picture 2
    3  <-- Rating for Picture 3
)

This is then saved to a .csv file using the following code: 然后使用以下代码将其保存到.csv文件:

-(void)saveRatings
{
    NSString *picRatings = [NSString stringWithFormat:@"%@, \n",self.rated];
    // Find documents directory
    NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *survey = [docPath stringByAppendingPathComponent:@"pictureRatings.csv"];
    // Create new file if none exists
    if (![[NSFileManager defaultManager] fileExistsAtPath:survey])
    {
        [[NSFileManager defaultManager] createFileAtPath:survey contents:nil attributes:nil];
    }
    NSFileHandle *fileHandle = [NSFileHandle fileHandleForUpdatingAtPath:survey];
    [fileHandle seekToEndOfFile];
    [fileHandle writeData:[picRatings dataUsingEncoding:NSUTF8StringEncoding]];
    [fileHandle closeFile];
}

I can then display the results of the .csv file to a UITextView, though it displays it exactly how the array is structured. 然后,我可以将.csv文件的结果显示到UITextView中,尽管它可以准确显示数组的结构。 For example, multiple results display as: 例如,多个结果显示为:

(
    1,
    2,
    3 
),
(
    1,
    2,
    3 
),
(
    1,
    2,
    3 
)

Is there a way I am able to format the array so that it is saved as 1,2,3? 有没有一种方法可以格式化数组,以便将其另存为1,2,3? And would I be able to add a column header like Picture 1, Picture 2, Picture 3? 我是否可以添加诸如图片1,图片2,图片3的列标题? For example, I would like to display results on the UITextView something like: 例如,我想在UITextView上显示结果,例如:

Picture 1, Picture 2, Picture 3
1,2,3
1,2,3
1,2,3

I've tried searching but can't seem to find this answer. 我尝试搜索,但似乎找不到此答案。 Any help is appreciated! 任何帮助表示赞赏! Thanks. 谢谢。

Edit: I have solved this thanks to jrturton's solution, using the following code: 编辑:由于jrturton的解决方案,我使用以下代码解决了此问题:

        // Set column titles for .csv
    NSArray *columnTitleArray = @[@"Picture 1", @"Picture 2", @"Picture 3"];

    NSString *columnTitle = [columnTitleArray componentsJoinedByString:@","];
    NSString *columnTitleToWrite = [columnTitle stringByAppendingString:@"\n"];

    // Separate ratings into cells
    NSString *picRatings = [rated componentsJoinedByString:@","];
    NSString *picRatingsToWrite = [picRatings stringByAppendingString:@"\n"];

    // Find documents directory
..

Then adding this to the method to make sure column headers are only set when a new file is created: 然后将其添加到方法中,以确保仅在创建新文件时才设置列标题:

    // Create new file if none exists
    if (![[NSFileManager defaultManager] fileExistsAtPath:survey])
    {
        [[NSFileManager defaultManager] createFileAtPath:survey contents:nil attributes:nil];

        // Set title for new file
        NSFileHandle *fileHandle = [NSFileHandle fileHandleForUpdatingAtPath:survey];
        [fileHandle writeData:[columnTitleToWrite dataUsingEncoding:NSUTF8StringEncoding]];
    }
..

Here is a much easier way: 这是一个简单得多的方法:

NSMutableString *csv = [NSMutableString string];

NSString *label = ...;

[csv appendFormat:@"%@,\n", label];

NSNumber *keyNum = ...;

[csv appendFormat:@"%d,%d\n", [keyNum intValue], [countNum intValue]];

NSString *filename = @"counts.csv";

NSError *error; 

[csv writeToFile:filename atomically:YES encoding:NSUTF8StringEncoding error:&error];

使用此解析器并将数据放入数组,并查看如何将数据用于UITabelViewCell https://github.com/davedelong/CHCSVParser

NSMutableString *csv = [NSMutableString string];

NSString *label = @"\n";

[csv appendFormat:@",", label];

NSString *picRatings = [rated componentsJoinedByString:csv];

I'm not sure what you expect appendFormat to be doing in the above code, this will probably be raising a warning since label is not used. 我不确定您在上述代码中期望appendFormat做什么,因为未使用label ,这可能会发出警告。 You don't need a mutable string in this case either. 在这种情况下,您也不需要可变的字符串。

To get the contents of an array separated by commas, do this: 要获取以逗号分隔的数组的内容,请执行以下操作:

NSString *picRatings = [rated componentsJoinedByString:@","];

You also need to add a new line to the end of this string, so you'd want: 您还需要在此字符串的末尾添加新行,因此需要:

NSString *picRatingsToWrite = [picRatings stringByAppendingString:@"\n"];

Write this string to your file and you should be fine. 将此字符串写入文件,就可以了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM