NSFileManager.defaultManager().createFileAtPath()返回false [英] NSFileManager.defaultManager().createFileAtPath() returning false

查看:362
本文介绍了NSFileManager.defaultManager().createFileAtPath()返回false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在经过一千个print()语句之后,我终于找到了问题所在!但是,我不确定如何修复它.问题出在行中:

After like a thousand print() statements, I have finally pinpointed the problem! However, I'm not sure how to fix it. The problem lies in the line:

NSFileManager.defaultManager().createFileAtPath(savePath, contents: data, attributes: nil)

根据Apple开发人员指南,此行代码returns true if the operation was successful or if the item already exists, otherwise false.

According to the Apple Developer's guide, this line of code returns true if the operation was successful or if the item already exists, otherwise false.

此行返回一个false,我不确定为什么,因为该行之前的代码似乎还可以.有人对如何解决此错误有任何建议吗?

This line is returning a false and I'm not exactly sure why because the code preceding the line seems to be okay. Anybody have any suggestions on how to solve this bug?

其余代码在这里:

//
//  ViewController.swift
//  Downloading An Image From The Web
//
//  Created by Jae Hyun Kim on 9/6/15.
//  Copyright © 2015 Jae Hyun Kim. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var image: UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()
        let url = NSURL(string: "http://7-themes.com/data_images/out/3/6776407-beautiful-scenery-pictures.jpg")
        let urlRequest = NSURLRequest(URL: url!)

        let task = NSURLSession.sharedSession().dataTaskWithRequest(urlRequest, completionHandler: { (data, response, error) -> Void in
            if error != nil {
                print(error)
            }
            else {
                if let bach = UIImage(data: data!) {
                    //self.image.image = bach
                    let documentsDirectory:String?
                    let paths:[AnyObject] = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.PicturesDirectory, NSSearchPathDomainMask.UserDomainMask, true)
                    print(paths)
                    if paths.count > 0 {
                        documentsDirectory = paths[0] as? String
                        let savePath = documentsDirectory! + "/bach.jpg"

                        print(NSFileManager.defaultManager().createFileAtPath(savePath, contents: data, attributes: nil))

                        if NSFileManager.defaultManager().fileExistsAtPath(savePath) {
                            print("file available")
                        }
                        else {
                            print("file not available")
                        }



                        self.image.image = UIImage(contentsOfFile: savePath)
                    }
                }
            }
        })
        task!.resume()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

推荐答案

import UIKit

class ViewController: UIViewController {
    @IBOutlet weak var image: UIImageView!
    override func viewDidLoad() {
        super.viewDidLoad()

        let url = NSURL(string: "http://7-themes.com/data_images/out/3/6776407-beautiful-scenery-pictures.jpg")!
        let urlRequest = NSURLRequest(URL: url)

        let task = NSURLSession.sharedSession().dataTaskWithRequest(urlRequest, completionHandler: { (data, response, error) -> Void in


            // you should always do it from the main queue otherwise you will experience a big delay when trying to display your image
            dispatch_async(dispatch_get_main_queue()) {
                // unwrap your data
                if let data = data {
                    print(data.length)
                    // get your caches directory URL
                    let cachesDirectory = try! NSFileManager().URLForDirectory(.CachesDirectory, inDomain: .UserDomainMask, appropriateForURL: nil, create: true)
                    // create your local file url by appending your url last path component
                    let fileUrl = cachesDirectory.URLByAppendingPathComponent(url.lastPathComponent!)
                    // save downloaded data to disk
                    if data.writeToURL(fileUrl, atomically: true) {
                        print(true)
                        // load your saved image from disk
                        self.image.image = UIImage(contentsOfFile: fileUrl.path!)
                    }

                }
            }

        })
        task.resume()
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

请注意,您需要按照以下步骤编辑plist:

Note you will need to edit your plist as follow:

这篇关于NSFileManager.defaultManager().createFileAtPath()返回false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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