Ios NSFileManager.defaultManager().createFileAtPath()返回false
经过上千条Ios NSFileManager.defaultManager().createFileAtPath()返回false,ios,xcode,swift2,Ios,Xcode,Swift2,经过上千条print()语句,我终于找到了问题所在!然而,我不知道如何修复它。问题在于: NSFileManager.defaultManager().createFileAtPath(保存路径,内容:数据,属性:nil) 根据《苹果开发者指南》,如果操作成功或项目已经存在,则这行代码返回true,否则返回false。 这一行返回了一个false,我不太清楚为什么,因为这一行前面的代码似乎没有问题。有人对如何解决这个bug有什么建议吗 代码的其余部分如下所示: // // ViewContro
print()
语句,我终于找到了问题所在!然而,我不知道如何修复它。问题在于:
NSFileManager.defaultManager().createFileAtPath(保存路径,内容:数据,属性:nil)
根据《苹果开发者指南》,如果操作成功或项目已经存在,则这行代码返回true,否则返回false。
这一行返回了一个false
,我不太清楚为什么,因为这一行前面的代码似乎没有问题。有人对如何解决这个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.
}
}
注意:您需要按如下方式编辑plist:
为什么不使用我在上一个问题中发布的代码?目录是否存在?如果没有,您可能需要创建它。请尝试将文件保存在
NSSearchPathDirectory.DocumentDirectory
中。让我知道。我之所以再次发布关于同一问题的帖子,是因为我想知道函数返回false的原因。我将使用你的代码。我相信你的代码比我发布的要好得多。考虑到Swift 2.0仍在betaOkay中,你应该用Swift 2.0标记你的问题。知道了。还有,我可以私下和你谈谈吗?我想问你一些关于iOS开发的问题。
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.
}
}