@@ -24,7 +24,7 @@ class YTKKeyValueItem_Swift:NSObject{
24
24
class YTKKeyValueStore_Swift : NSObject {
25
25
26
26
//文件夹路径
27
- let PATH_OF_DOCUMENT : NSString = NSSearchPathForDirectoriesInDomains ( NSSearchPathDirectory . DocumentDirectory, NSSearchPathDomainMask . UserDomainMask, true ) [ 0 ] as NSString
27
+ let PATH_OF_DOCUMENT : String = NSSearchPathForDirectoriesInDomains ( . DocumentDirectory, . UserDomainMask, true ) [ 0 ] as String
28
28
29
29
private var dbQueue : FMDatabaseQueue ?
30
30
@@ -39,18 +39,28 @@ class YTKKeyValueStore_Swift: NSObject {
39
39
let DELETE_ITEMS_WITH_PREFIX_SQL = " DELETE from %@ where id like ? "
40
40
41
41
42
- class func checkTableName( tableName : NSString ! ) -> Bool {
43
- if ( tableName. rangeOfString ( " " ) . location != NSNotFound) {
44
- println ( " error, table name: %@ format error " , tableName)
42
+ /**
43
+ 检查名字是否合法
44
+
45
+ :param: tableName 表名
46
+
47
+ :returns: 合法性
48
+ */
49
+ class func checkTableName( tableName : String ! ) -> Bool {
50
+ if find ( tableName, " " ) != nil {
51
+ println ( " error, table name: \( tableName) format error " )
45
52
return false
46
53
}
47
54
return true
48
55
}
49
56
57
+ //MARK: - 初始化
58
+
50
59
override init ( ) {
51
60
super. init ( )
52
61
self . setupDB ( DEFAULT_DB_NAME)
53
62
}
63
+
54
64
init ( dbName : String ! ) {
55
65
super. init ( )
56
66
self . setupDB ( dbName)
@@ -64,6 +74,9 @@ class YTKKeyValueStore_Swift: NSObject {
64
74
dbQueue = FMDatabaseQueue ( path: dbPath)
65
75
}
66
76
77
+
78
+ //MARK: - 数据库操作
79
+
67
80
/**
68
81
创建表单
69
82
@@ -79,7 +92,7 @@ class YTKKeyValueStore_Swift: NSObject {
79
92
result = db. executeUpdate ( sql, withArgumentsInArray: nil )
80
93
} )
81
94
if !result! {
82
- println ( " error, failed to create table: %@ " , tableName)
95
+ println ( " error, failed to create table: \( tableName) " )
83
96
}
84
97
}
85
98
@@ -98,7 +111,7 @@ class YTKKeyValueStore_Swift: NSObject {
98
111
result = db. executeUpdate ( sql, withArgumentsInArray: nil )
99
112
} )
100
113
if !result!{
101
- println ( " error, failed to clear table: %@ " , tableName)
114
+ println ( " error, failed to clear table: \( tableName) " )
102
115
}
103
116
}
104
117
@@ -208,8 +221,8 @@ class YTKKeyValueStore_Swift: NSObject {
208
221
*/
209
222
func getStringById( stringId : String ! , fromTable tableName : String ! ) -> String ? {
210
223
let array : AnyObject ? = self . getObjectById ( stringId, fromTable: tableName)
211
- if let result = array as? NSArray {
212
- return result [ 0 ] as? String
224
+ if let result = array as? [ String ] {
225
+ return result [ 0 ]
213
226
} else {
214
227
return nil
215
228
}
@@ -222,7 +235,7 @@ class YTKKeyValueStore_Swift: NSObject {
222
235
:param: numberId 索引
223
236
:param: tableName 表单名
224
237
*/
225
- func putNumber( number : NSNumber ! , withId numberId : String ! , intoTable tableName : String ! ) {
238
+ func putNumber( number : CGFloat ! , withId numberId : String ! , intoTable tableName : String ! ) {
226
239
self . putObject ( [ number] , withId: numberId, intoTable: tableName)
227
240
}
228
241
@@ -234,10 +247,10 @@ class YTKKeyValueStore_Swift: NSObject {
234
247
235
248
:returns: 数字
236
249
*/
237
- func getNumberById( numberId : String ! , fromTable tableName : String ! ) -> NSNumber ? {
250
+ func getNumberById( numberId : String ! , fromTable tableName : String ! ) -> CGFloat ? {
238
251
let array : AnyObject ? = self . getObjectById ( numberId, fromTable: tableName)
239
- if let result = array as? NSArray {
240
- return result [ 0 ] as? NSNumber
252
+ if let result = array as? [ CGFloat ] {
253
+ return result [ 0 ]
241
254
} else {
242
255
return nil
243
256
}
@@ -300,7 +313,7 @@ class YTKKeyValueStore_Swift: NSObject {
300
313
result = db. executeUpdate ( sql, withArgumentsInArray: [ objectId] )
301
314
} )
302
315
if !result! {
303
- println ( " error, failed to delete time from table: %@ " , tableName)
316
+ println ( " error, failed to delete time from table: \( tableName) " )
304
317
}
305
318
}
306
319
@@ -314,14 +327,13 @@ class YTKKeyValueStore_Swift: NSObject {
314
327
if !YTKKeyValueStore_Swift. checkTableName ( tableName) {
315
328
return
316
329
}
317
- var stringBuilder = NSMutableString ( )
330
+ var stringBuilder = " "
318
331
for objectId in objectIdArray{
319
332
var item = " ' \( objectId) ' "
320
- if stringBuilder. length == 0 {
321
- stringBuilder. appendString ( " item " )
333
+ if stringBuilder. isEmpty {
334
+ stringBuilder += " item "
322
335
} else {
323
- stringBuilder. appendString ( " , " )
324
- stringBuilder. appendString ( item)
336
+ stringBuilder += " , \( item) "
325
337
}
326
338
}
327
339
let sql = NSString ( format: DELETE_ITEMS_SQL, tableName, stringBuilder)
@@ -330,7 +342,7 @@ class YTKKeyValueStore_Swift: NSObject {
330
342
result = db. executeUpdate ( sql, withArgumentsInArray: nil )
331
343
} )
332
344
if !result!{
333
- println ( " error, failed to delete items by ids from table: %@ " , tableName)
345
+ println ( " error, failed to delete items by ids from table: \( tableName) " )
334
346
}
335
347
}
336
348
@@ -345,13 +357,13 @@ class YTKKeyValueStore_Swift: NSObject {
345
357
return
346
358
}
347
359
let sql = NSString ( format: DELETE_ITEMS_WITH_PREFIX_SQL, tableName)
348
- let prefixArgument = NSString ( format : " %@%% " , objectIdPrefix)
360
+ let prefixArgument = " \( objectIdPrefix) % "
349
361
var result : Bool ?
350
362
dbQueue? . inDatabase ( { ( db) -> Void in
351
- result = db. executeUpdate ( sql, withArgumentsInArray: nil )
363
+ result = db. executeUpdate ( sql, withArgumentsInArray: [ prefixArgument ] )
352
364
} )
353
365
if !result!{
354
- println ( " error, failed to delete items by id prefix from table: %@ " , tableName)
366
+ println ( " error, failed to delete items by id prefix from table: \( tableName) " )
355
367
}
356
368
}
357
369
0 commit comments