目标C:在csv文件上写字符串

时间:2011-04-07 15:44:58

标签: objective-c nsstring nsmutablestring

  

可能重复:
  objective C: write in a csv (txt) string contained in some textfield

我有这个代码在csv文件(txt)中写一个字符串

NSMutableString *csvString = [NSMutableString stringWithString:textfield1.text];
[csvString appendString:@"#"];
[csvString appendString:textfield2.text];
[csvString appendString:@"#"];
[csvString appendString:dtextfield3.text];
[csvString appendString:@"#"];
[csvString appendString:textfield4.text];
[csvString appendString:@"#"];
[csvString appendString:textfield5.text];
[csvString appendString:@"#"];
[csvString appendString:textfield6.text];
[csvString appendString:@"#"];
[csvString appendString:textfield7.text];
[csvString appendString:@"#"];
if (uiswitch.on) { //switch
[csvString appendString:@"1"];
}
else [csvString appendString:@"0"];
[csvString appendString:@";"];

NSString *resourceFileName = @"Client";
NSString *pathToFile =[[NSBundle mainBundle] pathForResource: resourceFileName ofType: @"txt"];
NSError *error;

NSString *fileString = [NSString stringWithContentsOfFile:pathToFile encoding:NSUTF8StringEncoding error:&error];
if (!fileString) {
NSLog(@"Error reading file.");
} 

[csvString writeToFile:pathToFile atomically:YES encoding:NSUTF8StringEncoding error:nil];

但它不起作用,为什么???

0 个答案:

没有答案