字符串未写入文件 - iPhone
我在 iPhone 中使用以下代码将字符串写入存储在我的 iPhone 项目资源文件夹中的文件。当我尝试读取它时,它成功读取数据,但当我尝试写入时,它不写入文件,尽管它也没有给我任何错误。
这是我的代码:
NSString *myString; //Assuume the string you want to write is this
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"myfile.txt"];
[myString writeToFile:path atomically:YES];
请任何人指导。
I am using the following code in iPhone to write a string into the file that is stored in my iPhone Project Resource Folder. When i try to read its reading the data successfully but when i try to write its not writing the file although its aloso not giving me any error.
this is my code:
NSString *myString; //Assuume the string you want to write is this
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"myfile.txt"];
[myString writeToFile:path atomically:YES];
Please can anybody guide.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
writeToFile:atomically: 在 iOS 2.0 中已被弃用。使用 writeToFile:atomically:encoding:error: 代替。您应该检查字符串的内容,例如通过 NSLog() 函数。不适当的初始化可能会导致失败。
The writeToFile:atomically: has been depredecated in iOS 2.0. Use writeToFile:atomically:encoding:error: instead. You should check the content of the string, for example by NSLog() function. Inappropriate initialization can cause a failure.
只是我的两分钱,来自 OS X,我习惯于调用
writeToURL()
而不是writeToFile()
。事实证明,writeToFile()
是适用于 iOS 的。Just my two cents, coming from OS X, I am used to calling
writeToURL()
instead ofwriteToFile()
. Turns out,writeToFile()
is the one that worked for iOS.