将代码转换为swift 2

时间:2015-10-04 10:03:00

标签: swift

我有一个代码

func pathForBuggyWKWebView(filePath: String?) -> String? {
    let fileMgr = NSFileManager.defaultManager()
    let tmpPath = NSTemporaryDirectory().stringByAppendingPathComponent("www")
    var error: NSErrorPointer = nil
    if !fileMgr.createDirectoryAtPath(tmpPath, withIntermediateDirectories: true, attributes: nil, error: error) {
        println("Couldn't create www subdirectory. \(error)")
        return nil
    }
    let dstPath = tmpPath.stringByAppendingPathComponent(filePath!.lastPathComponent)
    if !fileMgr.fileExistsAtPath(dstPath) {
        if !fileMgr.copyItemAtPath(filePath!, toPath: dstPath, error: error) {
            println("Couldn't copy file to /tmp/www. \(error)")
            return nil
        }
    }
    return dstPath
}

错误低于

enter image description here

我尝试使用此解决方案

stringByAppendingPathComponent is unavailable

我设法用

解决了第一个错误
let tmpPath = NSURL(fileURLWithPath: NSTemporaryDirectory()).URLByAppendingPathComponent("www")

错误号码4我试图用代码解决

let writePath = NSURL((fileURLWithPath: tmpPath).URLByAppendingPathComponent(filePath!.lastPathComponent))

但它确实有效。

也许有人可以帮助解决2,3,4的错误?

3 个答案:

答案 0 :(得分:1)

Swift 2不再支持可选的NSError参数, 你应该使用try catch代替

do {
   try fileMgr.createDirectoryAtPath(tmpPath, withIntermediateDirectories: true, attributes: nil)

   catch{
     //handle the error...
   }
}

答案 1 :(得分:1)

func pathForBuggyWKWebView(filePath: String?) -> String? {
    let fileMgr = NSFileManager.defaultManager()
    let tmpPath = NSTemporaryDirectory().stringByAppendingPathComponent("www")
    do {
        try fileMgr.createDirectoryAtPath(tmpPath, withIntermediateDirectories: true, attributes: nil)
        // do whatever you want here if createDirectoryAtPath successful
        let dstPath = tmpPath.stringByAppendingPathComponent(filePath!.lastPathComponent)
        if !fileMgr.fileExistsAtPath(dstPath) {
            do {
                try fileMgr.copyItemAtPath(filePath!, toPath: dstPath)
                // do whatever you want here if copyItemAtPath was successful
            } catch let error as NSError {
                print("Couldn't copy file to /tmp/www. \(error.localizedDescription)")
                return nil
            }
        }
        return dstPath
    } catch let error as NSError {
        print("Couldn't create www subdirectory. \(error.localizedDescription)")
        return nil
    }
}

使用Swift 2.0 stringByAppendingPathComponent仅适用于NSStrings,因此您需要强制转换为NSString或添加此扩展以将stringByAppendingPathComponent与String结合使用。

extension String {
    var nsValue: NSString {
        return self
    }
    func stringByAppendingPathComponent(component: String) -> String {
        return nsValue.stringByAppendingPathComponent(component)
    }
}

答案 2 :(得分:1)

最佳做法是使用基于URL的文件路径而不是基于字符串的文件路径。

要使用网址,请尝试:

func urlForBuggyWKWebView(filePath: String?) -> NSURL? {
    guard
        let path = filePath,
        let fileURL = NSURL(string: path),
        let lastPathComponent = fileURL.lastPathComponent
        else { return nil }

    let fileManager = NSFileManager.defaultManager()
    let tempURL = NSURL.fileURLWithPath(NSTemporaryDirectory(), isDirectory: true).URLByAppendingPathComponent("www")
    do {
        try fileManager.createDirectoryAtURL(tempURL, withIntermediateDirectories: true, attributes: nil)
    } catch {
        print("Can't create directory: \(error)")
        return nil
    }

    let destinationURL = tempURL.URLByAppendingPathComponent(lastPathComponent)

    if !destinationURL.checkResourceIsReachableAndReturnError(nil) {
        do {
            try fileManager.copyItemAtURL(fileURL, toURL: destinationURL)
        } catch {
            print("Unable to copy file: \(error)")
            return nil
        }
    }

    return destinationURL
}