首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSURLDownload:断言失败([路径isAbsolutePath])故障

NSURLDownload:断言失败([路径isAbsolutePath])故障
EN

Stack Overflow用户
提问于 2015-09-06 23:04:46
回答 1查看 281关注 0票数 0

我正在尝试从因特网下载一个文件,并将它放在应用程序支持目录下的应用程序名称目录中,我一直得到一个

代码语言:javascript
复制
Assertion failed: ([path isAbsolutePath]), function -[NSURLDownload     setDestination:allowOverwrite:], file /SourceCache/CFNetwork/CFNetwork-720.5.7/Foundation/NSURLDownload.mm, line 370.

下面是我写的代码:

代码语言:javascript
复制
    var imageRequest = NSURLRequest(URL: self.source)
    var imageDownload = NSURLDownload(request: imageRequest, delegate:self)
    var error: NSError? = NSError()

    /* does path exist */
    let directoryPath = self.destination.stringByDeletingLastPathComponent
    let fileMgr = NSFileManager();
    fileMgr.createDirectoryAtPath(directoryPath, withIntermediateDirectories: true, attributes: nil, error: &error)
    imageDownload.setDestination(self.destination, allowOverwrite: true);

当我跨过代码时,一切看起来都是正确的。(https:/remoteDomain.com/img/downloadimage.jpg)是self.source是一个NSURL

(file:/Users/ryan/Library/Application%20Support/AppName/downloadimage.jpg)是我的系统self.destination中的完整路径。

有什么想法吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-09-07 04:09:54

要回答特定主题的问题:错误消息显示路径无效。创建图像路径的正确方法如下:

代码语言:javascript
复制
let fileManager = NSFileManager.defaultManager()

var folder = "~/Library/Application Support/[APPNAME]/someFolder" as NSString
folder = folder.stringByExpandingTildeInPath

if fileManager.fileExistsAtPath(folder as String) == false {
    do {
        try fileManager.createDirectoryAtPath(folder as String, withIntermediateDirectories: true, attributes: nil)
    }

    catch {
       //Deal with the error
    }
}

,但 @jtbandes是对的。您应该使用NSURLSessionDownloadTask下载您的文件。它是Foundation.framework的一部分,它可以在OS、iOS和watchOS上使用。

使用它的原因是苹果不断更新这款Api,以满足最新的标准。例如,您不需要担心IPv4或IPv6等,这样可以避免应用程序中的崩溃和怪异行为。

这就是你如何使用它(Swift):

代码语言:javascript
复制
var imageRequest = NSURLRequest(URL: self.source)
let session = NSURLSession.sharedSession()
let downloadTask = session.downloadTaskWithRequest(imageRequest) { (url: NSURL?, response: NSURLResponse?, error: NSError?) -> Void in
    //Work with data
}

downloadTask.resume()

注意,url是下载的映像的路径。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32429123

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档