Skip to content

Commit ebbd94d

Browse files
committed
UPDATE: 更新说明文件。
MOD: 修改方法变量权限。
1 parent b6fb4db commit ebbd94d

File tree

4 files changed

+136
-31
lines changed

4 files changed

+136
-31
lines changed

Diff for: README.md

+96-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,99 @@
1-
1+
YTKKeyValueStore_Swfit
2+
==========
23

34
`YTKKeyValueStore`的swift版本
45

5-
源oc版地址:https://github.com/yuantiku/YTKKeyValueStore
6+
源oc版地址:https://github.com/yuantiku/YTKKeyValueStore
7+
8+
![License MIT](https://go-shields.herokuapp.com/license-MIT-blue.png)
9+
10+
## 使用示例
11+
12+
```swift
13+
var store = YTKKeyValueStore_Swift(dbName: "test_siwft.db")
14+
let tableName = "user_table_swift"
15+
store.createTable(tableName: tableName)
16+
//保存
17+
let key = "1"
18+
let user = ["id":1 , "name" : "tangqiao" , "age" : 30]
19+
store.putObject(user, withId: key, intoTable: tableName)
20+
//查询
21+
if let queryUser: AnyObject = store.getObjectById(key, fromTable: tableName){
22+
println("[swift] query data result: \(queryUser)")
23+
}
24+
```
25+
26+
## 使用说明
27+
28+
所有的接口都封装在`YTKKeyValueStore_Swift.swift`类中。以下是一些常用方法说明。
29+
30+
### 打开(或创建)数据库
31+
32+
通过`init(dbName:)`方法,即可在程序的`Document`目录打开指定的数据库文件。如果该文件不存在,则会创建一个新的数据库。
33+
34+
```swift
35+
// 打开名为test.db的数据库,如果该文件不存在,则创新一个新的。
36+
var store = YTKKeyValueStore_Swift(dbName: "test.db")
37+
```
38+
39+
### 创建数据库表
40+
41+
通过`createTable(tableName:)`方法,我们可以在打开的数据库中创建表,如果表名已经存在,则会忽略该操作。如下所示:
42+
43+
```swift
44+
var store = YTKKeyValueStore_Swift(dbName: "test.db")
45+
let tableName = "user_table"
46+
// 创建名为user_table的表,如果已存在,则忽略该操作
47+
store.createTable(tableName: tableName)
48+
```
49+
50+
### 读写数据
51+
52+
`YTKKeyValueStore_Swift`类提供key-value的存储接口,存入的所有数据需要提供key以及其对应的value,读取的时候需要提供key来获得相应的value。
53+
54+
`YTKKeyValueStore_Swift`类支持的value类型包括:String, CGFloat, Dictionary和Array以及对应的oc类型,为此提供了以下接口:
55+
56+
```
57+
putString(string:withId:intoTable:)
58+
putNumber(number:withId:intoTable:)
59+
putObject(objct:withId:intoTable:)
60+
```
61+
62+
与此对应,有以下value为String, CGFloat, Dictionary和Array的读取接口:
63+
64+
```
65+
getStringById(stringId:fromTable:)->String?
66+
getNumberById(numberId:fromTable:)->CGFloat?
67+
getObjectById(objectId:fromTable:)->AnyObject?
68+
```
69+
70+
### 删除数据接口
71+
72+
`YTKKeyValueStore_Swift`提供了以下接口用于删除数据。
73+
74+
```
75+
// 清除数据表中所有数据
76+
clearTable(#tableName:)
77+
78+
// 删除指定key的数据
79+
deleteObjectById(objectId:fromTable:)
80+
81+
// 批量删除一组key数组的数据
82+
deleteObjectsByIdArray(objectIdArray:fromTable:)
83+
84+
// 批量删除所有带指定前缀的数据
85+
deleteObjectsByIdPrefix(objectIdfix:fromTable:)
86+
```
87+
88+
### 更多接口
89+
90+
`YTKKeyValueStore_Swift`还提供了以下接口来获取表示内部存储的key-value对象。
91+
92+
```
93+
// 获得指定key的数据
94+
getYTKKeyValueItemById(objectId:fromTable:)->YTKKeyValueItem_Seift?
95+
// 获得所有数据
96+
getAllItemsFromTable(tableName:)->[AnyObject]?
97+
```
98+
99+
由于`YTKKeyValueItem_Swift`类带有`createdTime`字段,可以获得该条数据的插入(或更新)时间,以便上层做复杂的处理(例如用来做缓存过期逻辑)。
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Bucket
3+
type = "1"
4+
version = "2.0">
5+
</Bucket>

Diff for: YTKKeyValueStore_Swift/YTKKeyValueStore_Swift.swift

+35-29
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import UIKit
1010

1111

12-
class YTKKeyValueItem_Swift:NSObject{
12+
public class YTKKeyValueItem_Swift:NSObject{
1313
var itemId : String?
1414
var itemObject : AnyObject?
1515
var createdTime : NSDate?
@@ -20,23 +20,22 @@ class YTKKeyValueItem_Swift:NSObject{
2020

2121
}
2222

23-
24-
class YTKKeyValueStore_Swift: NSObject {
23+
public class YTKKeyValueStore_Swift: NSObject {
2524

2625
//文件夹路径
27-
let PATH_OF_DOCUMENT : String = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as String
26+
private let PATH_OF_DOCUMENT : String = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as String
2827

2928
private var dbQueue : FMDatabaseQueue?
3029

31-
let DEFAULT_DB_NAME = "database_swift.sqlite"
32-
let CREATE_TABLE_SQL = "CREATE TABLE IF NOT EXISTS %@ ( id TEXT NOT NULL, json TEXT NOT NULL, createdTime TEXT NOT NULL, PRIMARY KEY(id)) "
33-
let UPDATE_ITEM_SQL = "REPLACE INTO %@ (id, json, createdTime) values (?, ?, ?)"
34-
let QUERY_ITEM_SQL = "SELECT json, createdTime from %@ where id = ? Limit 1"
35-
let SELECT_ALL_SQL = "SELECT * from %@"
36-
let CLEAR_ALL_SQL = "DELETE from %@"
37-
let DELETE_ITEM_SQL = "DELETE from %@ where id = ?"
38-
let DELETE_ITEMS_SQL = "DELETE from %@ where id in ( %@ )"
39-
let DELETE_ITEMS_WITH_PREFIX_SQL = "DELETE from %@ where id like ? "
30+
private let DEFAULT_DB_NAME = "database_swift.sqlite"
31+
private let CREATE_TABLE_SQL = "CREATE TABLE IF NOT EXISTS %@ ( id TEXT NOT NULL, json TEXT NOT NULL, createdTime TEXT NOT NULL, PRIMARY KEY(id)) "
32+
private let UPDATE_ITEM_SQL = "REPLACE INTO %@ (id, json, createdTime) values (?, ?, ?)"
33+
private let QUERY_ITEM_SQL = "SELECT json, createdTime from %@ where id = ? Limit 1"
34+
private let SELECT_ALL_SQL = "SELECT * from %@"
35+
private let CLEAR_ALL_SQL = "DELETE from %@"
36+
private let DELETE_ITEM_SQL = "DELETE from %@ where id = ?"
37+
private let DELETE_ITEMS_SQL = "DELETE from %@ where id in ( %@ )"
38+
private let DELETE_ITEMS_WITH_PREFIX_SQL = "DELETE from %@ where id like ? "
4039

4140

4241
/**
@@ -46,7 +45,7 @@ class YTKKeyValueStore_Swift: NSObject {
4645

4746
:returns: 合法性
4847
*/
49-
class func checkTableName(tableName : String!)->Bool{
48+
private class func checkTableName(tableName : String!)->Bool{
5049
if find(tableName, " ") != nil{
5150
println("error, table name: \(tableName) format error")
5251
return false
@@ -82,7 +81,7 @@ class YTKKeyValueStore_Swift: NSObject {
8281

8382
:param: tableName 表单名
8483
*/
85-
func createTable(#tableName:String!){
84+
public func createTable(#tableName:String!){
8685
if !YTKKeyValueStore_Swift.checkTableName(tableName) {
8786
return
8887
}
@@ -101,7 +100,7 @@ class YTKKeyValueStore_Swift: NSObject {
101100

102101
:param: tableName 表单名
103102
*/
104-
func clearTable(#tableName:String!){
103+
public func clearTable(#tableName:String!){
105104
if !YTKKeyValueStore_Swift.checkTableName(tableName){
106105
return
107106
}
@@ -115,14 +114,16 @@ class YTKKeyValueStore_Swift: NSObject {
115114
}
116115
}
117116

117+
//MARK: 对象
118+
118119
/**
119120
加入数据
120121

121122
:param: object 数据
122123
:param: objectId 数据索引
123124
:param: tableName 表单名
124125
*/
125-
func putObject(object : AnyObject! , withId objectId: String! , intoTable tableName: String!){
126+
public func putObject(object : AnyObject! , withId objectId: String! , intoTable tableName: String!){
126127
if !YTKKeyValueStore_Swift.checkTableName(tableName){
127128
return
128129
}
@@ -151,7 +152,7 @@ class YTKKeyValueStore_Swift: NSObject {
151152

152153
:returns: 对象数据
153154
*/
154-
func getObjectById(objectId : String! , fromTable tableName : String! )->AnyObject?{
155+
public func getObjectById(objectId : String! , fromTable tableName : String! )->AnyObject?{
155156
let item = self.getYTKKeyValueItemById(objectId, fromTable: tableName)
156157
if item != nil {
157158
return item!.itemObject
@@ -167,7 +168,7 @@ class YTKKeyValueStore_Swift: NSObject {
167168

168169
:returns: 对象数据
169170
*/
170-
func getYTKKeyValueItemById(objectId :String! , fromTable tableName : String! )->YTKKeyValueItem_Swift?{
171+
public func getYTKKeyValueItemById(objectId :String! , fromTable tableName : String! )->YTKKeyValueItem_Swift?{
171172
if !YTKKeyValueStore_Swift.checkTableName(tableName){
172173
return nil
173174
}
@@ -199,6 +200,7 @@ class YTKKeyValueStore_Swift: NSObject {
199200
}
200201
}
201202

203+
//MARK: 字符串
202204

203205
/**
204206
插入字符串
@@ -207,7 +209,7 @@ class YTKKeyValueStore_Swift: NSObject {
207209
:param: stringId 索引
208210
:param: tableName 表单名
209211
*/
210-
func putString(string : String! , withId stringId : String! , intoTable tableName:String!){
212+
public func putString(string : String! , withId stringId : String! , intoTable tableName:String!){
211213
self.putObject([string], withId: stringId, intoTable: tableName)
212214
}
213215

@@ -219,7 +221,7 @@ class YTKKeyValueStore_Swift: NSObject {
219221

220222
:returns: 字符串
221223
*/
222-
func getStringById(stringId : String! , fromTable tableName : String!)->String?{
224+
public func getStringById(stringId : String! , fromTable tableName : String!)->String?{
223225
let array : AnyObject? = self.getObjectById(stringId, fromTable: tableName)
224226
if let result = array as? [String]{
225227
return result[0]
@@ -228,14 +230,16 @@ class YTKKeyValueStore_Swift: NSObject {
228230
}
229231
}
230232

233+
//MARK: 数组
234+
231235
/**
232236
插入数字
233237

234238
:param: number 数字
235239
:param: numberId 索引
236240
:param: tableName 表单名
237241
*/
238-
func putNumber(number : CGFloat! , withId numberId : String! , intoTable tableName : String!){
242+
public func putNumber(number : CGFloat! , withId numberId : String! , intoTable tableName : String!){
239243
self.putObject([number], withId: numberId, intoTable: tableName)
240244
}
241245

@@ -247,7 +251,7 @@ class YTKKeyValueStore_Swift: NSObject {
247251

248252
:returns: 数字
249253
*/
250-
func getNumberById(numberId : String! , fromTable tableName : String!)->CGFloat?{
254+
public func getNumberById(numberId : String! , fromTable tableName : String!)->CGFloat?{
251255
let array : AnyObject? = self.getObjectById(numberId, fromTable: tableName)
252256
if let result = array as? [CGFloat] {
253257
return result[0]
@@ -256,14 +260,16 @@ class YTKKeyValueStore_Swift: NSObject {
256260
}
257261
}
258262

263+
//MARK: 其他
264+
259265
/**
260266
获取表单的所有的数据
261267

262268
:param: tableName 表单名
263269

264270
:returns: 所有数据
265271
*/
266-
func getAllItemsFromTable(tableName : String!)->[AnyObject]?{
272+
public func getAllItemsFromTable(tableName : String!)->[AnyObject]?{
267273
if !YTKKeyValueStore_Swift.checkTableName(tableName){
268274
return nil
269275
}
@@ -298,12 +304,12 @@ class YTKKeyValueStore_Swift: NSObject {
298304
}
299305

300306
/**
301-
根据所以删除数据
307+
根据索引删除数据
302308

303309
:param: objectId 索引
304310
:param: tableName 表单名
305311
*/
306-
func deleteObjectById(objectId : String! , fromTable tableName:String!){
312+
public func deleteObjectById(objectId : String! , fromTable tableName:String!){
307313
if !YTKKeyValueStore_Swift.checkTableName(tableName){
308314
return
309315
}
@@ -323,7 +329,7 @@ class YTKKeyValueStore_Swift: NSObject {
323329
:param: objectIdArray 索引数组
324330
:param: tableName 表单名
325331
*/
326-
func deleteObjectsByIdArray(objectIdArray:[AnyObject]! , fromTable tableName : String!){
332+
public func deleteObjectsByIdArray(objectIdArray:[AnyObject]! , fromTable tableName : String!){
327333
if !YTKKeyValueStore_Swift.checkTableName(tableName){
328334
return
329335
}
@@ -352,7 +358,7 @@ class YTKKeyValueStore_Swift: NSObject {
352358
:param: objectIdPrefix 索引前缀
353359
:param: tableName 表单名
354360
*/
355-
func deleteObjectsByIdPrefix(objectIdPrefix :String , fromTable tableName:String){
361+
public func deleteObjectsByIdPrefix(objectIdPrefix :String , fromTable tableName:String){
356362
if !YTKKeyValueStore_Swift.checkTableName(tableName){
357363
return
358364
}
@@ -370,7 +376,7 @@ class YTKKeyValueStore_Swift: NSObject {
370376
/**
371377
关闭数据库
372378
*/
373-
func close(){
379+
public func close(){
374380
dbQueue?.close()
375381
dbQueue = nil
376382
}

0 commit comments

Comments
 (0)