目标C:写入包含在一些文本字段中的csv(txt)字符串 [英] objective C: write in a csv (txt) string contained in some textfield

查看:113
本文介绍了目标C:写入包含在一些文本字段中的csv(txt)字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中,我有一个视图,我在某些文本字段中写字,当我按下按钮这些字符串必须存储在csv文件中,如下例所示:(5个文本框的示例)

In my project I have a view where I write words in some textfield, when I press a button these string must be stored in a csv file as this example: (example with 5 textfield)

firststring#secondstring#thirdstring#fourthstring#fifthstring;

这是我想要的结果的一个例子。我如何做?

this is an example of the result that I want. How can I do?

编辑以添加

字符串的代码

 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:@";"];

finally csvString

finally csvString

NSLog(@"string = %@", csvString);

正是我的字符串

推荐答案

除了任何CSV格式问题,它也看起来像你试图写入不允许的主包。

它可以在模拟器,但不是在设备。

请参阅 iOS应用程序编程指南

Aside from any CSV format issues, it also looks like you're trying to write to the main bundle which is not allowed.
It may work in the simulator but not on the device.
See the Security and The File System sections in the iOS Application Programming Guide.

您需要在tmp或Documents文件夹中创建文件。例如:

You'll need to create the file in the tmp or Documents folder instead. For example:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDir = [paths objectAtIndex:0];
NSString *filename = [docDir stringByAppendingPathComponent:@"Client.txt"]; 
NSError *error = NULL;
BOOL written = [csvString writeToFile:filename atomically:YES encoding:NSUTF8StringEncoding error:&error];
if (!written)
    NSLog(@"write failed, error=%@", error);

这篇关于目标C:写入包含在一些文本字段中的csv(txt)字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆