0

I have a code

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
}

with errors below

enter image description here

I tried to use this solution

stringByAppendingPathComponent is unavailable

and I managed to solve first error with

let tmpPath = NSURL(fileURLWithPath: NSTemporaryDirectory()).URLByAppendingPathComponent("www")

Error number 4 i tried to solve with code

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

but it doest work.

Maybe somebody can help with errors 2,3, 4 ?

Community
  • 1
  • 1
Alexey K
  • 6,537
  • 18
  • 60
  • 118
  • 1
    Possible duplicate of [stringByAppendingPathComponent is unavailable](http://stackoverflow.com/questions/32501627/stringbyappendingpathcomponent-is-unavailable) – Eric Aya Oct 04 '15 at 10:44

3 Answers3

1

Swift 2 doesn't support optional NSError argument anymore, you should use try catch instead

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

   catch{
     //handle the error...
   }
}
Daniel Krom
  • 9,751
  • 3
  • 43
  • 44
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
    }
}

With Swift 2.0 stringByAppendingPathComponent is only available to NSStrings, so you need to cast to NSString or add this extension to use stringByAppendingPathComponent with String.

extension String {
    var nsValue: NSString {
        return self
    }
    func stringByAppendingPathComponent(component: String) -> String {
        return nsValue.stringByAppendingPathComponent(component)
    }
}
Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
1

Best practices have been to use URL based file paths rather than string based file paths.

To use URLs, try:

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
}
Abizern
  • 146,289
  • 39
  • 203
  • 257