【问题标题】:writetofile always returns falsewritetofile 总是返回 false
【发布时间】:2016-03-07 05:31:11
【问题描述】:

我正在尝试快速将内容写入文件

let jsonResult = try!   NSJSONSerialization.JSONObjectWithData(response.data, options:   NSJSONReadingOptions.MutableContainers) as! NSDictionary   

let fileManager = NSFileManager.defaultManager()
let urls = fileManager.URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)

let documentDirectory = urls.first!.path
let path = NSURL(fileURLWithPath: documentDirectory!).URLByAppendingPathComponent("data.txt")
let dict = jsonResult as NSDictionary
let status = dict.writeToFile(path.path!, atomically: false)
print(status)

但内容未写入文件,状态始终为假

【问题讨论】:

    标签: ios swift swift2 xcode7


    【解决方案1】:

    斯威夫特 2

            let file = "file.txt" //this is the file. we will write to and read from it
    
            let jsonResult : NSMutableDictionary = NSMutableDictionary.init(object: "08:00:00", forKey: "start_hour");
            jsonResult.setValue("10:00:00", forKey: "end_hour");
            jsonResult.setValue("30", forKey: "call_duration");
    
            let dict = jsonResult as NSDictionary
    
            if let dir : NSString = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.AllDomainsMask, true).first {
                let path = NSURL(fileURLWithPath: dir as String).URLByAppendingPathComponent(file);
    
                //writing
                dict.writeToFile(path.path!, atomically: false)
    
                var contents : NSString
                //reading
                do {
                    contents = try NSString(contentsOfFile: path.path!, encoding: NSUTF8StringEncoding)
                }
                catch {
                    /* error handling here */
                    contents  = ""
                }
                print(contents as String);
            }
    

    如果您已经有一个文件在捆绑包中,使用以下代码查找路径并读取文件。

        let path = NSBundle.mainBundle().pathForResource("sample-text", ofType: "txt")
    
        let contents: NSString
        do {
            contents = try NSString(contentsOfFile: path!, encoding: NSUTF8StringEncoding)
        } catch _ {
            contents = ""
        }
    

    解析 Web 服务响应:

            // responseData - API response data.
    
            // parse the result as JSON, since that's what the API provides
            let getConfig: NSDictionary
            do {
                 getConfig = try NSJSONSerialization.JSONObjectWithData(responseData, options: NSJSONReadingOptions()) as! NSDictionary
            } catch  {
                print("error trying to convert data to JSON")
            }
    

    【讨论】:

    • stringByAppendingPathComponent 在 xcode 7(已弃用)中不起作用。这就是我使用 URLByAppendingPathComponent 的原因。
    • 我使用的是 xcode 7.1。我没有得到弃用的问题。但是,我已经更新了代码。
    • 你测试过上面的例子吗?它解决了你的问题@jithin 吗?
    • 是的,这适用于您的 json 内容,但不适用于我的 json 数据。
    • @jithin,你在处理网络服务响应数据吗?
    【解决方案2】:

    您无法写入应用程序的捆绑包。相反,请尝试写入应用程序的文档目录。看到这个问题:Write a file on iOS

    【讨论】:

    • @Aron Wojnowski 我先试过了。请看我修改后的问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-20
    • 2021-11-05
    • 2018-11-05
    • 2019-05-06
    • 2017-04-29
    • 2013-04-30
    • 2015-01-20
    相关资源
    最近更新 更多