Skip to content

Commit 07e5e38

Browse files
committed
UPDATE: 0.2.0
1 parent 1749fa5 commit 07e5e38

File tree

5 files changed

+188
-149
lines changed

5 files changed

+188
-149
lines changed

README.md

+18-7
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,21 @@ YTKKeyValueStore_Swfit
77

88
![License MIT](https://go-shields.herokuapp.com/license-MIT-blue.png)
99

10+
## 关于更新
11+
12+
在不修改数据库结构的情况下优化了源版本提供的读写接口,去除了`putString`,`putNumber`,`getString`,`getNumber`的接口,统计集成在`putObject``getObject`中。写入时会自动判断并插入,读出时返回一个`YTKObject`对象,提供了以下属性读取相关的数据:
13+
14+
```
15+
objectValue : 读取出AnyObject?的类型
16+
stringValue : 读取出String?的类型
17+
numberValue : 读取出NSNumber?的类型
18+
dictionaryValue : 读取出Dictionary<String , AnyObject>?的类型
19+
arrayValue : 读取出Array<AnyObject>?的类型
20+
```
21+
`YTKKeyValueItem_Swift``itemObject`也修改为`YTKObject`属性方便读取。
22+
23+
旧的版本在`releases 0.1.0`中。地址: [YTKKeyValueStore_Swift(0.1.0)](https://github.com/sgxiang/YTKKeyValueStore_Swift/archive/0.1.0.zip)
24+
1025
## 使用示例
1126

1227
```swift
@@ -18,7 +33,7 @@ let key = "1"
1833
let user = ["id":1 , "name" : "tangqiao" , "age" : 30]
1934
store.putObject(user, withId: key, intoTable: tableName)
2035
//查询
21-
if let queryUser: AnyObject = store.getObjectById(key, fromTable: tableName){
36+
if let queryUser: AnyObject = store.getObjectById(key, fromTable: tableName)?.dictionaryValue{
2237
println("[swift] query data result: \(queryUser)")
2338
}
2439
```
@@ -54,17 +69,13 @@ store.createTable(tableName: tableName)
5469
`YTKKeyValueStore_Swift`类支持的value类型包括:String, CGFloat, Dictionary和Array以及对应的oc类型,为此提供了以下接口:
5570

5671
```
57-
putString(string:withId:intoTable:)
58-
putNumber(number:withId:intoTable:)
5972
putObject(objct:withId:intoTable:)
6073
```
6174

6275
与此对应,有以下value为String, CGFloat, Dictionary和Array的读取接口:
6376

6477
```
65-
getStringById(stringId:fromTable:)->String?
66-
getNumberById(numberId:fromTable:)->CGFloat?
67-
getObjectById(objectId:fromTable:)->AnyObject?
78+
getObjectById(objectId:fromTable:)->YTKObject?
6879
```
6980

7081
### 删除数据接口
@@ -93,7 +104,7 @@ deleteObjectsByIdPrefix(objectIdfix:fromTable:)
93104
// 获得指定key的数据
94105
getYTKKeyValueItemById(objectId:fromTable:)->YTKKeyValueItem_Swift?
95106
// 获得所有数据
96-
getAllItemsFromTable(tableName:)->[AnyObject]?
107+
getAllItemsFromTable(tableName:)->[YTKKeyValueItem_Swift]?
97108
```
98109

99110
由于`YTKKeyValueItem_Swift`类带有`createdTime`字段,可以获得该条数据的插入(或更新)时间,以便上层做复杂的处理(例如用来做缓存过期逻辑)。

YTKKeyValueStore_Swift/AppDelegate.swift

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,12 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
1919
let tableName = "user_table_swift"
2020
var store = YTKKeyValueStore_Swift(dbName: "test_siwft.db")
2121
store.createTable(tableName: tableName)
22+
store.clearTable(tableName: tableName)
2223
let key = "1"
2324
let user = ["id":1 , "name" : "tangqiao" , "age" : 30]
2425
store.putObject(user, withId: key, intoTable: tableName)
2526

26-
if let queryUser: AnyObject = store.getObjectById(key, fromTable: tableName){
27+
if let queryUser: AnyObject = store.getObjectById(key, fromTable: tableName)?.dictionaryValue{
2728
println("[swift] query data result: \(queryUser)")
2829
}
2930

YTKKeyValueStore_Swift/YTKKeyValueStore_Swift.swift

+131-101
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ import UIKit
1010

1111

1212
public class YTKKeyValueItem_Swift:NSObject{
13-
var itemId : String?
14-
var itemObject : AnyObject?
15-
var createdTime : NSDate?
13+
public var itemId : String?
14+
public var itemObject : YTKObject?
15+
public var createdTime : NSDate?
1616

17-
func description() -> String{
17+
public func description() -> String{
1818
return "id=\(itemId), value=\(itemObject), timeStamp=\(createdTime)"
1919
}
2020

@@ -116,31 +116,56 @@ public class YTKKeyValueStore_Swift: NSObject {
116116

117117
//MARK: 对象
118118

119+
private enum YTKKeyValueType{
120+
case String,Number,Object
121+
}
122+
123+
private class func valueWithType(object : AnyObject!)->YTKKeyValueType{
124+
if object is String{
125+
return .String
126+
}else if object as? NSNumber != nil{
127+
return .Number
128+
}else{
129+
return .Object
130+
}
131+
}
132+
119133
/**
120134
加入数据
121135

122136
:param: object 数据
123137
:param: objectId 数据索引
124138
:param: tableName 表单名
125139
*/
140+
126141
public func putObject(object : AnyObject! , withId objectId: String! , intoTable tableName: String!){
127142
if !YTKKeyValueStore_Swift.checkTableName(tableName){
128143
return
129144
}
130-
var error : NSError?
131-
var data = NSJSONSerialization.dataWithJSONObject(object, options: NSJSONWritingOptions(0), error: &error)
132-
if error != nil {
133-
println("error, faild to get json data")
134-
return
145+
146+
let type = YTKKeyValueStore_Swift.valueWithType(object)
147+
var jsonString : String?
148+
149+
if type == .Number || type == .Object{
150+
let sqlObject: AnyObject! = type == .Number ? [object] : object
151+
var error : NSError?
152+
let data = NSJSONSerialization.dataWithJSONObject(sqlObject, options: NSJSONWritingOptions(0), error: &error)
153+
if error != nil {
154+
println("error, faild to get json data")
155+
return
156+
}
157+
jsonString = NSString(data: data!, encoding: NSUTF8StringEncoding)
135158
}else{
136-
let jsonString = NSString(data: data!, encoding: NSUTF8StringEncoding)
137-
let createTime = NSDate()
138-
let sql = NSString(format: UPDATE_ITEM_SQL, tableName)
139-
var result : Bool?
140-
dbQueue?.inDatabase({ (db) -> Void in
141-
result = db.executeUpdate(sql, withArgumentsInArray:[objectId,jsonString!,createTime])
142-
})
159+
jsonString = object as? String
143160
}
161+
162+
let createTime = NSDate()
163+
let sql = NSString(format: UPDATE_ITEM_SQL, tableName)
164+
var result : Bool?
165+
dbQueue?.inDatabase({ (db) -> Void in
166+
result = db.executeUpdate(sql, withArgumentsInArray:[objectId,jsonString!,createTime])
167+
})
168+
144169
}
145170

146171

@@ -152,7 +177,7 @@ public class YTKKeyValueStore_Swift: NSObject {
152177

153178
:returns: 对象数据
154179
*/
155-
public func getObjectById(objectId : String! , fromTable tableName : String! )->AnyObject?{
180+
public func getObjectById(objectId : String! , fromTable tableName : String! )->YTKObject?{
156181
let item = self.getYTKKeyValueItemById(objectId, fromTable: tableName)
157182
if item != nil {
158183
return item!.itemObject
@@ -184,81 +209,16 @@ public class YTKKeyValueStore_Swift: NSObject {
184209
rs.close()
185210
})
186211
if json != nil{
187-
var error : NSError?
188-
var result: AnyObject? = NSJSONSerialization.JSONObjectWithData(json!.dataUsingEncoding(NSUTF8StringEncoding)!, options: NSJSONReadingOptions.AllowFragments, error: &error)
189-
if error != nil{
190-
println("error, faild to prase to json")
191-
return nil
192-
}
193212
var item = YTKKeyValueItem_Swift()
194213
item.itemId = objectId
195-
item.itemObject = result!
214+
item.itemObject = YTKObject(value: json! )
196215
item.createdTime = createdTime
197216
return item
198217
}else{
199218
return nil
200219
}
201220
}
202221

203-
//MARK: 字符串
204-
205-
/**
206-
插入字符串
207-
208-
:param: string 字符串
209-
:param: stringId 索引
210-
:param: tableName 表单名
211-
*/
212-
public func putString(string : String! , withId stringId : String! , intoTable tableName:String!){
213-
self.putObject([string], withId: stringId, intoTable: tableName)
214-
}
215-
216-
/**
217-
获取字符串
218-
219-
:param: stringId 索引
220-
:param: tableName 表单名
221-
222-
:returns: 字符串
223-
*/
224-
public func getStringById(stringId : String! , fromTable tableName : String!)->String?{
225-
let array : AnyObject? = self.getObjectById(stringId, fromTable: tableName)
226-
if let result = array as? [String]{
227-
return result[0]
228-
}else{
229-
return nil
230-
}
231-
}
232-
233-
//MARK: 数组
234-
235-
/**
236-
插入数字
237-
238-
:param: number 数字
239-
:param: numberId 索引
240-
:param: tableName 表单名
241-
*/
242-
public func putNumber(number : CGFloat! , withId numberId : String! , intoTable tableName : String!){
243-
self.putObject([number], withId: numberId, intoTable: tableName)
244-
}
245-
246-
/**
247-
获取数字
248-
249-
:param: numberId 索引
250-
:param: tableName 表单名
251-
252-
:returns: 数字
253-
*/
254-
public func getNumberById(numberId : String! , fromTable tableName : String!)->CGFloat?{
255-
let array : AnyObject? = self.getObjectById(numberId, fromTable: tableName)
256-
if let result = array as? [CGFloat] {
257-
return result[0]
258-
}else{
259-
return nil
260-
}
261-
}
262222

263223
//MARK: 其他
264224

@@ -269,38 +229,25 @@ public class YTKKeyValueStore_Swift: NSObject {
269229

270230
:returns: 所有数据
271231
*/
272-
public func getAllItemsFromTable(tableName : String!)->[AnyObject]?{
232+
public func getAllItemsFromTable(tableName : String!)->[YTKKeyValueItem_Swift]?{
273233
if !YTKKeyValueStore_Swift.checkTableName(tableName){
274234
return nil
275235
}
276236
let sql = NSString(format: SELECT_ALL_SQL, tableName)
277-
var result : [AnyObject] = []
237+
var result : [YTKKeyValueItem_Swift] = []
278238
dbQueue?.inDatabase({ (db) -> Void in
279239
var rs : FMResultSet = db.executeQuery(sql, withArgumentsInArray: nil)
280240
while(rs.next()){
281241
var item = YTKKeyValueItem_Swift()
282242
item.itemId = rs.stringForColumn("id")
283-
item.itemObject = rs.stringForColumn("json")
243+
item.itemObject = YTKObject(value:rs.stringForColumn("json"))
284244
item.createdTime = rs.dateForColumn("createdTime")
285245
result.append(item)
286246
}
287247
rs.close()
288248
})
289-
var error : NSError?
290-
291-
for i in 0..<result.count {
292-
var item: YTKKeyValueItem_Swift = result[i] as YTKKeyValueItem_Swift
293-
error = nil
294-
var object: AnyObject? = NSJSONSerialization.JSONObjectWithData(item.itemObject!.dataUsingEncoding(NSUTF8StringEncoding)!, options: NSJSONReadingOptions.AllowFragments, error: &error)
295-
if error != nil {
296-
println("error, faild to prase to json.")
297-
}else{
298-
item.itemObject = object!
299-
result[i] = item
300-
}
301-
}
302-
303-
return result
249+
250+
return result == [] ? nil : result
304251
}
305252

306253
/**
@@ -383,3 +330,86 @@ public class YTKKeyValueStore_Swift: NSObject {
383330

384331
}
385332

333+
//MARK: - 数据库的对象类型
334+
335+
public struct YTKObject{
336+
private var value : AnyObject?
337+
338+
public var objectValue : AnyObject?{
339+
get{
340+
return self.value
341+
}
342+
}
343+
344+
public var stringValue : String? {
345+
get{
346+
return self.value as? String
347+
}
348+
}
349+
350+
public var numberValue : NSNumber?{
351+
352+
get{
353+
if self.value == nil { return nil}
354+
355+
if let num = self.value as? NSNumber{
356+
return num
357+
}else{
358+
359+
var error : NSError?
360+
let result: AnyObject? = NSJSONSerialization.JSONObjectWithData(self.value!.dataUsingEncoding(NSUTF8StringEncoding)!, options: NSJSONReadingOptions.AllowFragments, error: &error)
361+
if error != nil{
362+
return nil
363+
}else{
364+
if let num = result as? [NSNumber]{
365+
return num[0]
366+
}else{
367+
return nil
368+
}
369+
}
370+
}
371+
}
372+
373+
}
374+
375+
public var dictionaryValue : Dictionary<String , AnyObject>?{
376+
get{
377+
if self.value == nil { return nil}
378+
379+
var error : NSError?
380+
let result: AnyObject? = NSJSONSerialization.JSONObjectWithData(self.value!.dataUsingEncoding(NSUTF8StringEncoding)!, options: NSJSONReadingOptions.AllowFragments, error: &error)
381+
if error != nil{
382+
return nil
383+
}else{
384+
if let dic = result as? Dictionary<String , AnyObject>{
385+
return dic
386+
}else{
387+
return nil
388+
}
389+
}
390+
391+
}
392+
}
393+
394+
395+
public var arrayValue : Array<AnyObject>?{
396+
get{
397+
if self.value == nil { return nil}
398+
399+
var error : NSError?
400+
let result: AnyObject? = NSJSONSerialization.JSONObjectWithData(self.value!.dataUsingEncoding(NSUTF8StringEncoding)!, options: NSJSONReadingOptions.AllowFragments, error: &error)
401+
if error != nil{
402+
return nil
403+
}else{
404+
if let dic = result as? Array<AnyObject>{
405+
return dic
406+
}else{
407+
return nil
408+
}
409+
}
410+
}
411+
}
412+
413+
414+
}
415+

0 commit comments

Comments
 (0)