Answers:
看来该方法stringByAppendingPathComponent
已在Swift 2.0中删除,因此错误消息建议使用:
let writePath = NSURL(fileURLWithPath: NSTemporaryDirectory()).URLByAppendingPathComponent("instagram.igo")
更新:
URLByAppendingPathComponent()
已被替换为appendingPathComponent()
:
let writePath = NSURL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("instagram.igo")
stringByAppendingPathComponent
,请参阅下面的答案。
NSURL
方法,而是一个NSString
stringByAppendingPathComponent
在Swift 2.0中被删除”是不正确的,并且@Maysam并没有要求仅提供NSURL
方法。
它正在工作,NSString
因此您可以像这样使用它:
extension String {
func stringByAppendingPathComponent(path: String) -> String {
let nsSt = self as NSString
return nsSt.stringByAppendingPathComponent(path)
}
}
现在,您可以使用此扩展名,该扩展名将首先转换String
为NSString
,然后再执行操作。
您的代码将是:
let writePath = NSTemporaryDirectory().stringByAppendingPathComponent("instagram.igo")
这是其他一些使用方法:
extension String {
var lastPathComponent: String {
return (self as NSString).lastPathComponent
}
var pathExtension: String {
return (self as NSString).pathExtension
}
var stringByDeletingLastPathComponent: String {
return (self as NSString).stringByDeletingLastPathComponent
}
var stringByDeletingPathExtension: String {
return (self as NSString).stringByDeletingPathExtension
}
var pathComponents: [String] {
return (self as NSString).pathComponents
}
func stringByAppendingPathComponent(path: String) -> String {
let nsSt = self as NSString
return nsSt.stringByAppendingPathComponent(path)
}
func stringByAppendingPathExtension(ext: String) -> String? {
let nsSt = self as NSString
return nsSt.stringByAppendingPathExtension(ext)
}
}
参考从这里。
对于Swift 3.0:
extension String {
func stringByAppendingPathComponent1(path: String) -> String {
let nsSt = self as NSString
return nsSt.appendingPathComponent(path)
}
}
let writePath = NSTemporaryDirectory().stringByAppendingPathComponent(path: "instagram.igo")
extension String {
var lastPathComponent: String {
return (self as NSString).lastPathComponent
}
var pathExtension: String {
return (self as NSString).pathExtension
}
var stringByDeletingLastPathComponent: String {
return (self as NSString).deletingLastPathComponent
}
var stringByDeletingPathExtension: String {
return (self as NSString).deletingPathExtension
}
var pathComponents: [String] {
return (self as NSString).pathComponents
}
func stringByAppendingPathComponent(path: String) -> String {
let nsSt = self as NSString
return nsSt.appendingPathComponent(path)
}
func stringByAppendingPathExtension(ext: String) -> String? {
let nsSt = self as NSString
return nsSt.appendingPathExtension(ext)
}
}
NSURL
s 来查找资源的路径,而应使用s。只是说。
checkResourceIsReachable()
或checkPromisedItemIsReachable()
上URL
代替。FileManager
仍然是一个ObjC类NSFileManager
,NS
为Swift删除了前缀,并且fileExistsAtPath
从OS X 10.0开始就存在。此后世界已经发生了变化,并且随着应用被沙盒化(在iOS上不太明显),该文件可能存在,您可能没有查看权限。文件也可能在云中等。这就是为什么将简单BOOL
方法替换为的更复杂URL
但语义更正确的原因。
只需将字符串包装为即可NSString
。
let writePath = (NSTemporaryDirectory() as NSString).stringByAppendingPathComponent("instagram.igo")
String
class没有这个,但是NSString
存在!说得通。
对于Swift 3:
let writePath = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(directoryname).path
或更好地创建此扩展:
extension String {
func appendingPathComponent(_ string: String) -> String {
return URL(fileURLWithPath: self).appendingPathComponent(string).path
}
}
用法:
let writePath = NSTemporaryDirectory().appendingPathComponent(directoryname)
Swift 3解决方案:
这是获取文档目录路径的功能-
func getDocumentsDirectory() -> URL {
let paths = FileManager.default.urls(for: .documentDirectory, in:.userDomainMask)
let documentsDirectory = paths[0]
return documentsDirectory
}
如何使用:
getDocumentsDirectory.appendingPathComponent("google.com")
结果:
file:///var/folders/w1/3rcp2fvs1qv43hfsh5876s0h0000gn/T/com.apple.dt.Xcode.pg/containers/com.apple.dt.playground.stub.iOS_Simulator.MyPlayground-7CF9F706-509C-4D4C-997E-AB8FE9E4A6EA/Documents/google.com
快速2.0
// Get the documents Directory
func documentsDirectory() -> String {
let documentsFolderPath = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)[0]
return documentsFolderPath
}
// Get path for a file in the directory
func fileInDocumentsDirectory(filename: String) -> String {
let writePath = (documentsDirectory() as NSString).stringByAppendingPathComponent("Mobile")
if (!NSFileManager.defaultManager().fileExistsAtPath(writePath)) {
do {
try NSFileManager.defaultManager().createDirectoryAtPath(writePath, withIntermediateDirectories: false, attributes: nil) }
catch let error as NSError {
print(error.localizedDescription);
}
}
return (writePath as NSString).stringByAppendingPathComponent(filename)
}
//# MARK: - Save Image in Doc dir
func saveImage (image: UIImage, path: String ) -> Bool{
let pngImageData = UIImagePNGRepresentation(image)
// let jpgImageData = UIImageJPEGRepresentation(image, 1.0) // if you want to save as JPEG
let result = pngImageData!.writeToFile(path, atomically: true)
print("\(result)")
print("\(path)")
return result
}
您可以改用URLByAppendingPathComponent()。请注意,您应该修剪路径字符串以删除“ file://”前缀:
let uniqueFileName = NSUUID().UUIDString
let documentsDirectory = getDocumentsDirectoryURL()
if let path = documentsDirectory?.URLByAppendingPathComponent(uniqueFileName) {
var pathString = path.absoluteString
pathString = imagePathString.stringByTrimmingCharactersInSet(NSCharacterSet(charactersInString: "file://"))
}
func getDocumentsDirectoryURL() -> NSURL? {
let fileManager = NSFileManager()
if let docsDirectory = fileManager.URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).first {
return docsDirectory
}
return nil
}
请执行下列操作:
(("\(fileName)" as NSString).lastPathComponent as NSString).stringByDeletingPathExtension
斯威夫特4
extension String {
var lastPathComponent: String {
return (self as NSString).lastPathComponent
}
var pathExtension: String {
return (self as NSString).pathExtension
}
var stringByDeletingLastPathComponent: String {
return (self as NSString).deletingLastPathComponent
}
var stringByDeletingPathExtension: String {
return (self as NSString).deletingPathExtension
}
var pathComponents: [String] {
return (self as NSString).pathComponents
}
func stringByAppendingPathComponent(path: String) -> String {
let nsSt = self as NSString
return nsSt.appendingPathComponent(path)
}
func stringByAppendingPathExtension(ext: String) -> String? {
let nsSt = self as NSString
return nsSt.appendingPathExtension(ext)
}
}
Application%20Support