ryanfowler / SwiftData

Simple and Effective SQLite Handling in Swift
MIT License
517 stars 93 forks source link

please help on docpaths #30

Closed davestucky closed 8 years ago

davestucky commented 8 years ago

I am getting this error:"'stringByAppendingPathComponent' is unavailable: Use URLByAppendingPathComponent on NSURL instead." in swift data for the image directories mainly here: public static func saveUIImage(image: UIImage) -> String? {

    let docsPath = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)[0]
    let imageDirPath = NSURL(docsPath.URLByAppendingPathComponent("SwiftDataImages"))
    if !NSFileManager.defaultManager().fileExistsAtPath(imageDirPath) {
        do {
            try NSFileManager.defaultManager().createDirectoryAtPath(imageDirPath, withIntermediateDirectories: false, attributes: nil)
        } catch _ {
            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

}

/**
 Convenience function to delete a UIImage with the specified ID

 :param: id  The id of the UIImage

 :returns:   True if the image was successfully deleted, or false if there was an error during the deletion
 */
public static func deleteUIImageWithID(id: String) -> Bool {

    let docsPath = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)[0]
    let imageDirPath = docsPath.stringByAppendingPathComponent("SwiftDataImages")
    let fullPath = imageDirPath.stringByAppendingPathComponent(id)
    do {
        try NSFileManager.defaultManager().removeItemAtPath(fullPath)
        return true
    } catch _ as NSError {
        return false
    }

}

-------------------------------------------and here class func createPath() -> String {

        let docsPath = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)[0]
        let databaseStr = "SwiftData.sqlite"
        let dbPath = docsPath.stringByAppendingPathComponent(databaseStr)
        return dbPath

-------------------------------------------and here public func asUIImage() -> UIImage? {

        if let path = value as? String{
            let docsPath = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)[0]
            let imageDirPath = docsPath.stringByAppendingPathComponent("SwiftDataImages")
            let fullPath = imageDirPath.stringByAppendingPathComponent(path)
            if !NSFileManager.defaultManager().fileExistsAtPath(fullPath) {
                print("SwiftData Error -> Invalid image ID provided")
                return nil
            }
            if let imageAsData = NSData(contentsOfFile: fullPath) {
                return UIImage(data: imageAsData)
            }
        }
        return nil

    }

}
RegisStGelais commented 8 years ago

I posted a fix in issue 27 and issue 28 https://github.com/ryanfowler/SwiftData/issues/27 https://github.com/ryanfowler/SwiftData/issues/28