ryanfowler / SwiftData

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

Swift 2 #28

Closed davestucky closed 9 years ago

davestucky commented 9 years ago

Will you be updating to swift 2? If so any eta?

Thanks

RegisStGelais commented 9 years ago

It work well under Swift 2, you just have to add this extention for the String class

extension String { func stringByAppendingPathComponent(path: String) -> String { let nsSt = self as NSString return nsSt.stringByAppendingPathComponent(path) } }

You also need to delete the sqlite2 library from your project tree and add libsqlite3.tbd in the "build phase" tab, in section "Link Binary With Libraries"

I use SwiftData with these changes on Xcode 7 and iOS 9 :)

davestucky commented 9 years ago

OK I put that in cause I saw ticket # 27 put still have a challenge. Where does extension String go?

RegisStGelais commented 9 years ago

Anywhere in your project. Create a new swift file in your project and put the class extention in it.

davestucky commented 9 years ago

I put it in the SwiftData.swift file I am getting an error

Cannot invoke 'createDirectoryAtPath' with an argument list of type '(String, withIntermediateDirectories: Bool, attributes: NilLiteralConvertible, error: NilLiteralConvertible)’

for line: if !NSFileManager.defaultManager().createDirectoryAtPath(imageDirPath, withIntermediateDirectories: false, attributes: nil, error: nil)

and

ERROR Type 'String' does not conform to protocol ‘SequenceType'

for lines: for char in sql

// MARK: - Escaping And Binding Functions

extension SwiftData.SQLiteDB {

func bind(objects: [AnyObject], toSQL sql: String) -> (string: String, error: Int?) {

    var newSql = ""
    var bindIndex = 0
    var i = false
    for char in sql {

here if char == "?" { if bindIndex > objects.count - 1 { print("SwiftData Error -> During: Object Binding") print(" -> Code: 201 - Not enough objects to bind provided") return ("", 201) } var obj = "" if i { if let str = objects[bindIndex] as? String { obj = escapeIdentifier(str) } else { print("SwiftData Error -> During: Object Binding") print(" -> Code: 203 - Object to bind as identifier must be a String at array location: (bindIndex)") return ("", 203) } newSql = newSql.substringToIndex(newSql.endIndex.predecessor()) } else { obj = escapeValue(objects[bindIndex]) } newSql += obj ++bindIndex } else { newSql.append(char) } if char == "i" { i = true } else if i { i = false } } if bindIndex != objects.count { print("SwiftData Error -> During: Object Binding") print(" -> Code: 202 - Too many objects to bind provided") return ("", 202) } return (newSql, nil)

}

//return escaped String value of AnyObject
func escapeValue(obj: AnyObject?) -> String {

    if let obj: AnyObject = obj {
        if obj is String {
            return "'\(escapeStringValue(obj as! String))'"
        }
        if obj is Double || obj is Int {
            return "\(obj)"
        }
        if obj is Bool {
            if obj as! Bool {
                return "1"
            } else {
                return "0"
            }
        }
        if obj is NSData {
            let str = "\(obj)"
            var newStr = ""

here for char in str { if char != "<" && char != ">" && char != " " { newStr.append(char) } } return "X'(newStr)'" } if obj is NSDate { let dateFormatter = NSDateFormatter() dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss" return "(escapeValue(dateFormatter.stringFromDate(obj as! NSDate)))" } if obj is UIImage { if let imageID = SD.saveUIImage(obj as! UIImage) { return "'(escapeStringValue(imageID))'" } print("SwiftData Warning -> Cannot save image, NULL will be inserted into the database") return "NULL" } print("SwiftData Warning -> Object \"(obj)\" is not a supported type and will be inserted into the database as NULL") return "NULL" } else { return "NULL" }

}

//return escaped String identifier
func escapeIdentifier(obj: String) -> String {
    return "\"\(escapeStringIdentifier(obj))\""
}

//escape string
func escapeStringValue(str: String) -> String {
    var escapedStr = ""

here for char in str { if char == "'" { escapedStr += "'" } escapedStr.append(char) } return escapedStr }

//escape string
func escapeStringIdentifier(str: String) -> String {
    var escapedStr = ""

here for char in str { if char == "\"" { escapedStr += "\"" } escapedStr.append(char) } return escapedStr }

} // MARK: - Work Around For Swift2 extension String { func stringByAppendingPathComponent(path: String) -> String { let nsSt = self as NSString return nsSt.stringByAppendingPathComponent(path) } }

Thank you for your assistance

On Sep 21, 2015, at 07:06, Regis St-Gelais notifications@github.com wrote:

Anywhere in your project. Create a new swift file in your project and put the class extention in it.

— Reply to this email directly or view it on GitHub https://github.com/ryanfowler/SwiftData/issues/28#issuecomment-141952589.

RegisStGelais commented 9 years ago

Looks like the compiler does not see your extention class. The best way is to add a new swift file in your project (I called mine StringExtention.swift) and put the class code in it

RegisStGelais commented 8 years ago

Have a look at issue #33

Eswaran95 commented 8 years ago

how to perform update operation in my project, because i can not found any update example README.MD