簡體   English   中英

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

[英]Format contents of NSMutableArray for .csv file

我有一個圖片幻燈片應用程序,該應用程序允許用戶對3張圖像進行評分。 等級被存儲在一個名為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
)

然后使用以下代碼將其保存到.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];
}

然后,我可以將.csv文件的結果顯示到UITextView中,盡管它可以准確顯示數組的結構。 例如,多個結果顯示為:

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

有沒有一種方法可以格式化數組,以便將其另存為1,2,3? 我是否可以添加諸如圖片1,圖片2,圖片3的列標題? 例如,我想在UITextView上顯示結果,例如:

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

我嘗試搜索,但似乎找不到此答案。 任何幫助表示贊賞! 謝謝。

編輯:由於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
..

然后將其添加到方法中,以確保僅在創建新文件時才設置列標題:

    // 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]];
    }
..

這是一個簡單得多的方法:

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];

我不確定您在上述代碼中期望appendFormat做什么,因為未使用label ,這可能會發出警告。 在這種情況下,您也不需要可變的字符串。

要獲取以逗號分隔的數組的內容,請執行以下操作:

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

您還需要在此字符串的末尾添加新行,因此需要:

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

將此字符串寫入文件,就可以了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM