[英]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.