0
votes

I am using SwiftData framework for sqlite database. When I updated the Xcode form 6.4 to 7.0.1 the swift data.swift file shows this error Cannot invoke 'createDirectoryAtPath' with an argument list of type '(String, withIntermediateDirectories: Bool, attributes: NilLiteralConvertible, error: NilLiteralConvertible)'. I have provided the code below.

public static func saveUIImage(image: UIImage) -> String? {

        let docsPath = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)[0] as String
        let imageDirPath = docsPath.stringByAppendingPathComponent("SwiftDataImages")
        if !NSFileManager.defaultManager().fileExistsAtPath(imageDirPath) {
            if !NSFileManager.defaultManager().createDirectoryAtPath(imageDirPath, withIntermediateDirectories: false, attributes: nil, error: nil) {
                print("Error creating SwiftData image folder")
                return nil
            }
        }
        let imageID = NSUUID().UUIDString
        let imagePath = imageDirPath.stringByAppendingPathComponent(imageID)
        let imageAsData = UIImagePNGRepresentation(image)
        if !imageAsData!.writeToFile(imagePath, atomically: true) {
            print("Error saving image")
            return nil
        }
        return imageID

    }
1

1 Answers

2
votes

createDirectoryAtPath syntax is changed like:

- (BOOL)createDirectoryAtPath:(NSString *)path withIntermediateDirectories:(BOOL)createIntermediates attributes:(nullable NSDictionary<NSString *, id> *)attributes error:(NSError **)error NS_AVAILABLE(10_5, 2_0);

So you can update your code like":

NSFileManager.defaultManager().createDirectoryAtPath(imageDirPath, withIntermediateDirectories: false, attributes: [:])

And you complete code with swift 2.0:

func saveUIImage(image: UIImage) -> String? {

    let docsPath = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)[0] as String
    let imageDirPath = (docsPath as NSString).stringByAppendingPathComponent("SwiftDataImages")
    if !NSFileManager.defaultManager().fileExistsAtPath(imageDirPath) {

        do{
            try NSFileManager.defaultManager().createDirectoryAtPath(imageDirPath, withIntermediateDirectories: false, attributes: [:])
        } catch {
            print("Error creating SwiftData image folder")
            return nil
        }
    }
    let imageID = NSUUID().UUIDString
    let imagePath = (imageDirPath as NSString).stringByAppendingPathComponent(imageID)
    let imageAsData = UIImagePNGRepresentation(image)
    if !imageAsData!.writeToFile(imagePath, atomically: true) {
        print("Error saving image")
        return nil
    }
    return imageID
}

Or you can use extension for stringByAppendingPathComponent because it is not available for String in 2.0.

extension String {

    func stringByAppendingPathComponent(path: String) -> String {

        let nsSt = self as NSString

        return nsSt.stringByAppendingPathComponent(path)
    }
}

And you can use it this way:

let imageDirPath = docsPath.stringByAppendingPathComponent("SwiftDataImages")
let imagePath = imageDirPath.stringByAppendingPathComponent(imageID)