diff --git a/README.md b/README.md index bfbea02..e8bed72 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,21 @@ YTKKeyValueStore_Swfit ![License MIT](https://go-shields.herokuapp.com/license-MIT-blue.png) +## 关于更新 + +在不修改数据库结构的情况下优化了源版本提供的读写接口,去除了`putString`,`putNumber`,`getString`,`getNumber`的接口,统计集成在`putObject`和`getObject`中。写入时会自动判断并插入,读出时返回一个`YTKObject`对象,提供了以下属性读取相关的数据: + +``` +objectValue : 读取出AnyObject?的类型 +stringValue : 读取出String?的类型 +numberValue : 读取出NSNumber?的类型 +dictionaryValue : 读取出Dictionary?的类型 +arrayValue : 读取出Array?的类型 +``` +`YTKKeyValueItem_Swift`的`itemObject`也修改为`YTKObject`属性方便读取。 + +旧的版本在`releases 0.1.0`中。地址: [YTKKeyValueStore_Swift(0.1.0)](https://github.com/sgxiang/YTKKeyValueStore_Swift/archive/0.1.0.zip) + ## 使用示例 ```swift @@ -18,7 +33,7 @@ let key = "1" let user = ["id":1 , "name" : "tangqiao" , "age" : 30] store.putObject(user, withId: key, intoTable: tableName) //查询 -if let queryUser: AnyObject = store.getObjectById(key, fromTable: tableName){ +if let queryUser: AnyObject = store.getObjectById(key, fromTable: tableName)?.dictionaryValue{ println("[swift] query data result: \(queryUser)") } ``` @@ -54,17 +69,13 @@ store.createTable(tableName: tableName) `YTKKeyValueStore_Swift`类支持的value类型包括:String, CGFloat, Dictionary和Array以及对应的oc类型,为此提供了以下接口: ``` -putString(string:withId:intoTable:) -putNumber(number:withId:intoTable:) putObject(objct:withId:intoTable:) ``` 与此对应,有以下value为String, CGFloat, Dictionary和Array的读取接口: ``` -getStringById(stringId:fromTable:)->String? -getNumberById(numberId:fromTable:)->CGFloat? -getObjectById(objectId:fromTable:)->AnyObject? +getObjectById(objectId:fromTable:)->YTKObject? ``` ### 删除数据接口 @@ -93,7 +104,7 @@ deleteObjectsByIdPrefix(objectIdfix:fromTable:) // 获得指定key的数据 getYTKKeyValueItemById(objectId:fromTable:)->YTKKeyValueItem_Swift? // 获得所有数据 -getAllItemsFromTable(tableName:)->[AnyObject]? +getAllItemsFromTable(tableName:)->[YTKKeyValueItem_Swift]? ``` 由于`YTKKeyValueItem_Swift`类带有`createdTime`字段,可以获得该条数据的插入(或更新)时间,以便上层做复杂的处理(例如用来做缓存过期逻辑)。 diff --git a/YTKKeyValueStore_Swift.xcodeproj/project.xcworkspace/xcuserdata/YSQ.xcuserdatad/UserInterfaceState.xcuserstate b/YTKKeyValueStore_Swift.xcodeproj/project.xcworkspace/xcuserdata/YSQ.xcuserdatad/UserInterfaceState.xcuserstate index 0342a69..a3e858b 100644 Binary files a/YTKKeyValueStore_Swift.xcodeproj/project.xcworkspace/xcuserdata/YSQ.xcuserdatad/UserInterfaceState.xcuserstate and b/YTKKeyValueStore_Swift.xcodeproj/project.xcworkspace/xcuserdata/YSQ.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/YTKKeyValueStore_Swift/AppDelegate.swift b/YTKKeyValueStore_Swift/AppDelegate.swift index 0008aea..a0ac3d2 100644 --- a/YTKKeyValueStore_Swift/AppDelegate.swift +++ b/YTKKeyValueStore_Swift/AppDelegate.swift @@ -19,11 +19,12 @@ class AppDelegate: UIResponder, UIApplicationDelegate { let tableName = "user_table_swift" var store = YTKKeyValueStore_Swift(dbName: "test_siwft.db") store.createTable(tableName: tableName) + store.clearTable(tableName: tableName) let key = "1" let user = ["id":1 , "name" : "tangqiao" , "age" : 30] store.putObject(user, withId: key, intoTable: tableName) - if let queryUser: AnyObject = store.getObjectById(key, fromTable: tableName){ + if let queryUser: AnyObject = store.getObjectById(key, fromTable: tableName)?.dictionaryValue{ println("[swift] query data result: \(queryUser)") } diff --git a/YTKKeyValueStore_Swift/YTKKeyValueStore_Swift.swift b/YTKKeyValueStore_Swift/YTKKeyValueStore_Swift.swift index 64f356a..0479b48 100644 --- a/YTKKeyValueStore_Swift/YTKKeyValueStore_Swift.swift +++ b/YTKKeyValueStore_Swift/YTKKeyValueStore_Swift.swift @@ -10,11 +10,11 @@ import UIKit public class YTKKeyValueItem_Swift:NSObject{ - var itemId : String? - var itemObject : AnyObject? - var createdTime : NSDate? + public var itemId : String? + public var itemObject : YTKObject? + public var createdTime : NSDate? - func description() -> String{ + public func description() -> String{ return "id=\(itemId), value=\(itemObject), timeStamp=\(createdTime)" } @@ -116,6 +116,20 @@ public class YTKKeyValueStore_Swift: NSObject { //MARK: 对象 + private enum YTKKeyValueType{ + case String,Number,Object + } + + private class func valueWithType(object : AnyObject!)->YTKKeyValueType{ + if object is String{ + return .String + }else if object as? NSNumber != nil{ + return .Number + }else{ + return .Object + } + } + /** 加入数据 @@ -123,24 +137,35 @@ public class YTKKeyValueStore_Swift: NSObject { :param: objectId 数据索引 :param: tableName 表单名 */ + public func putObject(object : AnyObject! , withId objectId: String! , intoTable tableName: String!){ if !YTKKeyValueStore_Swift.checkTableName(tableName){ return } - var error : NSError? - var data = NSJSONSerialization.dataWithJSONObject(object, options: NSJSONWritingOptions(0), error: &error) - if error != nil { - println("error, faild to get json data") - return + + let type = YTKKeyValueStore_Swift.valueWithType(object) + var jsonString : String? + + if type == .Number || type == .Object{ + let sqlObject: AnyObject! = type == .Number ? [object] : object + var error : NSError? + let data = NSJSONSerialization.dataWithJSONObject(sqlObject, options: NSJSONWritingOptions(0), error: &error) + if error != nil { + println("error, faild to get json data") + return + } + jsonString = NSString(data: data!, encoding: NSUTF8StringEncoding) }else{ - let jsonString = NSString(data: data!, encoding: NSUTF8StringEncoding) - let createTime = NSDate() - let sql = NSString(format: UPDATE_ITEM_SQL, tableName) - var result : Bool? - dbQueue?.inDatabase({ (db) -> Void in - result = db.executeUpdate(sql, withArgumentsInArray:[objectId,jsonString!,createTime]) - }) + jsonString = object as? String } + + let createTime = NSDate() + let sql = NSString(format: UPDATE_ITEM_SQL, tableName) + var result : Bool? + dbQueue?.inDatabase({ (db) -> Void in + result = db.executeUpdate(sql, withArgumentsInArray:[objectId,jsonString!,createTime]) + }) + } @@ -152,7 +177,7 @@ public class YTKKeyValueStore_Swift: NSObject { :returns: 对象数据 */ - public func getObjectById(objectId : String! , fromTable tableName : String! )->AnyObject?{ + public func getObjectById(objectId : String! , fromTable tableName : String! )->YTKObject?{ let item = self.getYTKKeyValueItemById(objectId, fromTable: tableName) if item != nil { return item!.itemObject @@ -184,15 +209,9 @@ public class YTKKeyValueStore_Swift: NSObject { rs.close() }) if json != nil{ - var error : NSError? - var result: AnyObject? = NSJSONSerialization.JSONObjectWithData(json!.dataUsingEncoding(NSUTF8StringEncoding)!, options: NSJSONReadingOptions.AllowFragments, error: &error) - if error != nil{ - println("error, faild to prase to json") - return nil - } var item = YTKKeyValueItem_Swift() item.itemId = objectId - item.itemObject = result! + item.itemObject = YTKObject(value: json! ) item.createdTime = createdTime return item }else{ @@ -200,65 +219,6 @@ public class YTKKeyValueStore_Swift: NSObject { } } - //MARK: 字符串 - - /** - 插入字符串 - - :param: string 字符串 - :param: stringId 索引 - :param: tableName 表单名 - */ - public func putString(string : String! , withId stringId : String! , intoTable tableName:String!){ - self.putObject([string], withId: stringId, intoTable: tableName) - } - - /** - 获取字符串 - - :param: stringId 索引 - :param: tableName 表单名 - - :returns: 字符串 - */ - public func getStringById(stringId : String! , fromTable tableName : String!)->String?{ - let array : AnyObject? = self.getObjectById(stringId, fromTable: tableName) - if let result = array as? [String]{ - return result[0] - }else{ - return nil - } - } - - //MARK: 数组 - - /** - 插入数字 - - :param: number 数字 - :param: numberId 索引 - :param: tableName 表单名 - */ - public func putNumber(number : CGFloat! , withId numberId : String! , intoTable tableName : String!){ - self.putObject([number], withId: numberId, intoTable: tableName) - } - - /** - 获取数字 - - :param: numberId 索引 - :param: tableName 表单名 - - :returns: 数字 - */ - public func getNumberById(numberId : String! , fromTable tableName : String!)->CGFloat?{ - let array : AnyObject? = self.getObjectById(numberId, fromTable: tableName) - if let result = array as? [CGFloat] { - return result[0] - }else{ - return nil - } - } //MARK: 其他 @@ -269,38 +229,25 @@ public class YTKKeyValueStore_Swift: NSObject { :returns: 所有数据 */ - public func getAllItemsFromTable(tableName : String!)->[AnyObject]?{ + public func getAllItemsFromTable(tableName : String!)->[YTKKeyValueItem_Swift]?{ if !YTKKeyValueStore_Swift.checkTableName(tableName){ return nil } let sql = NSString(format: SELECT_ALL_SQL, tableName) - var result : [AnyObject] = [] + var result : [YTKKeyValueItem_Swift] = [] dbQueue?.inDatabase({ (db) -> Void in var rs : FMResultSet = db.executeQuery(sql, withArgumentsInArray: nil) while(rs.next()){ var item = YTKKeyValueItem_Swift() item.itemId = rs.stringForColumn("id") - item.itemObject = rs.stringForColumn("json") + item.itemObject = YTKObject(value:rs.stringForColumn("json")) item.createdTime = rs.dateForColumn("createdTime") result.append(item) } rs.close() }) - var error : NSError? - - for i in 0..?{ + get{ + if self.value == nil { return nil} + + var error : NSError? + let result: AnyObject? = NSJSONSerialization.JSONObjectWithData(self.value!.dataUsingEncoding(NSUTF8StringEncoding)!, options: NSJSONReadingOptions.AllowFragments, error: &error) + if error != nil{ + return nil + }else{ + if let dic = result as? Dictionary{ + return dic + }else{ + return nil + } + } + + } + } + + + public var arrayValue : Array?{ + get{ + if self.value == nil { return nil} + + var error : NSError? + let result: AnyObject? = NSJSONSerialization.JSONObjectWithData(self.value!.dataUsingEncoding(NSUTF8StringEncoding)!, options: NSJSONReadingOptions.AllowFragments, error: &error) + if error != nil{ + return nil + }else{ + if let dic = result as? Array{ + return dic + }else{ + return nil + } + } + } + } + + +} + diff --git a/YTKKeyValueStore_SwiftTests/YTKKeyValueStore_SwiftTests.swift b/YTKKeyValueStore_SwiftTests/YTKKeyValueStore_SwiftTests.swift index 4260737..d407685 100644 --- a/YTKKeyValueStore_SwiftTests/YTKKeyValueStore_SwiftTests.swift +++ b/YTKKeyValueStore_SwiftTests/YTKKeyValueStore_SwiftTests.swift @@ -30,54 +30,51 @@ class YTKKeyValueStore_SwiftTests: XCTestCase { super.tearDown() } - func testSaveString(){ - - let str1 = "abc" - let key1 = "key1" - let str2 = "abc2" - let key2 = "key2" + func testSave(){ - _store?.putString(str1, withId: key1, intoTable: _tableName) - _store?.putString(str2, withId: key2, intoTable: _tableName) + let str = "abc" + let num1 = 1 + let num2 = 1.3 + let user : Dictionary = ["id":1 , "name" : "tangqiao" , "age" : 30] - var result : String? + _store?.putObject(str, withId: "str", intoTable: _tableName) + _store?.putObject(num1, withId: "num1", intoTable: _tableName) + _store?.putObject(num2, withId: "num2", intoTable: _tableName) + _store?.putObject(user, withId: "user", intoTable: _tableName) - result = _store?.getStringById(key1, fromTable: _tableName) - XCTAssertNotNil(result) - XCTAssertEqual(str1, result!) - result = _store?.getStringById(key2, fromTable: _tableName) - XCTAssertNotNil(result) - XCTAssertEqual(str2, result!) + if let result = _store?.getObjectById("str", fromTable: _tableName)?.stringValue{ + XCTAssertEqual(str, result) + }else{ + XCTAssertFalse(true) + } - result = _store?.getStringById("key3", fromTable: _tableName) - XCTAssertNil(result) + if let result = _store?.getObjectById("num1", fromTable: _tableName)?.numberValue{ + XCTAssertEqual(num1, result) + }else{ + XCTAssertFalse(true) + } - } - - func testSaveCGFloat(){ - - let num1 : CGFloat = 1 - let key1 = "key1" - let num2 : CGFloat = 2 - let key2 = "key2" - - _store?.putNumber(num1, withId: key1, intoTable: _tableName) - _store?.putNumber(num2, withId: key2, intoTable: _tableName) + if let result = _store?.getObjectById("num2", fromTable: _tableName)?.numberValue{ + XCTAssertEqual(num2, result) + }else{ + XCTAssertFalse(true) + } - var result : CGFloat? + if let result = _store?.getObjectById("user", fromTable: _tableName)?.dictionaryValue{ + XCTAssertEqual(user["id"] as Int, result["id"] as Int) + XCTAssertEqual(user["name"] as String, result["name"] as String) + XCTAssertEqual(user["age"] as Int, result["age"] as Int) + }else{ + XCTAssertFalse(true) + } - result = _store?.getNumberById(key1, fromTable: _tableName) - XCTAssertNotNil(result) - XCTAssertEqual(num1, result!) + if let result = _store?.getObjectById("user1", fromTable: _tableName)?.dictionaryValue{ + XCTAssertFalse(true) + }else{ + XCTAssertTrue(true) + } - result = _store?.getNumberById(key2, fromTable: _tableName) - XCTAssertNotNil(result) - XCTAssertEqual(num2, result!) - - result = _store?.getNumberById("key3", fromTable: _tableName) - XCTAssertNil(result) - } - + }