简体   繁体   English

writeToFile在模拟器上工作,而在使用iOS Swift的设备上不工作

[英]writeToFile is working on simulator and not working on device using ios swift

    if let filepath = NSBundle.mainBundle().pathForResource(“abc”, ofType: "txt")

    {
        var test_string=“abcd”
        let fileContent:NSData= test_string.dataUsingEncoding(NSUTF8StringEncoding)!
        fileContent.writeToFile(filepath, atomically: true)
        let contents = try NSString(contentsOfFile: filepath, usedEncoding: nil) as String
        print("file content  :  "+contents)
    }
    catch
    {
        self.view.makeToast("write to file error")
    }

Check Read and write data from text file . 选中从文本文件读取和写入数据

I think your problem is the path, because a simulator folder can be any where but for device no, it is better to save the file inside the folder of your apps itself. 我认为您的问题是路径,因为模拟器文件夹可以在任何位置,但对于设备编号则更好,最好将文件保存在应用程序本身的文件夹内。

And if you are writing text, you can just write try test_string.writeToFile(path, atomically: false, encoding: NSUTF8StringEncoding) 而且,如果您正在编写文本,则可以编写try test_string.writeToFile(path, atomically: false, encoding: NSUTF8StringEncoding)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM