问题描述
在iOS9之前,我们已经创建了一个这样的目录
Before iOS9, we had created a directory like so
let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as! String
let logsPath = documentsPath.stringByAppendingPathComponent("logs")
let errorPointer = NSErrorPointer()
NSFileManager.defaultManager().createDirectoryAtPath(logsPath, withIntermediateDirectories: true, attributes: nil, error: errorPointer)
但是在iOS9中,他们删除了String.stringByAppendingPathComponent.自动转换工具将我们对String的使用替换为NSURL. createDirectoryAtPath()需要一个字符串,因此我需要将NSURL转换为字符串.我们像这样使用absolutePath :(针对iOS9的更新)
But with iOS9 they removed String.stringByAppendingPathComponent. The auto convert tool replaced our use of String with NSURL. createDirectoryAtPath() takes a string so I need to convert the NSURL to a string. We used absolutePath like so: (update for iOS9)
let documentsPath = NSURL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0])
let logsPath = documentsPath.URLByAppendingPathComponent("logs")
do {
try NSFileManager.defaultManager().createDirectoryAtPath(logsPath.absoluteString, withIntermediateDirectories: true, attributes: nil)
} catch let error as NSError {
NSLog("Unable to create directory \(error.debugDescription)")
}
但是我遇到以下错误:
推荐答案
我想出了这一点. createDirectoryAtPath()无法处理带有"file://"前缀的路径.要获取不带前缀的路径,必须使用path()或relativePath().
I figured this one out. createDirectoryAtPath() is unable to process a path with the "file://" prefix. To get a path without the prefix you must use path() or relativePath().
let documentsPath = NSURL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0])
let logsPath = documentsPath.URLByAppendingPathComponent("logs")
do {
try NSFileManager.defaultManager().createDirectoryAtPath(logsPath.path!, withIntermediateDirectories: true, attributes: nil)
} catch let error as NSError {
NSLog("Unable to create directory \(error.debugDescription)")
}
不正确的路径(注意文件://):
Incorrect path (notice file://):
file:///var/mobile/Containers/Data/Application/F2EF2D4F-94AF-4BF2-AF9E-D0ECBC8637E7/Documents/logs/
file:///var/mobile/Containers/Data/Application/F2EF2D4F-94AF-4BF2-AF9E-D0ECBC8637E7/Documents/logs/
正确的路径:
/var/mobile/容器/数据/应用程序/F2EF2D4F-94AF-4BF2-AF9E-D0ECBC8637E7/Documents/logs/
/var/mobile/Containers/Data/Application/F2EF2D4F-94AF-4BF2-AF9E-D0ECBC8637E7/Documents/logs/
这篇关于iOS9 Swift文件使用NSURL创建NSFileManager.createDirectoryAtPath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!