mirror of
https://github.com/hyperoslo/Cache.git
synced 2026-04-07 19:17:36 +00:00
34 lines
729 B
Swift
34 lines
729 B
Swift
import Foundation
|
|
|
|
// MARK: - Cachable
|
|
|
|
extension JSON: Cachable {
|
|
|
|
public typealias CacheType = JSON
|
|
|
|
public static func decode(data: NSData) -> CacheType? {
|
|
var result: CacheType?
|
|
|
|
do {
|
|
let object = try NSJSONSerialization.JSONObjectWithData(data,
|
|
options: NSJSONReadingOptions())
|
|
|
|
switch (object) {
|
|
case let dictionary as [String : AnyObject]:
|
|
result = JSON.Dictionary(dictionary)
|
|
case let array as [AnyObject]:
|
|
result = JSON.Array(array)
|
|
default:
|
|
result = nil
|
|
}
|
|
} catch {}
|
|
|
|
return result
|
|
}
|
|
|
|
public func encode() -> NSData? {
|
|
return try? NSJSONSerialization.dataWithJSONObject(object,
|
|
options: NSJSONWritingOptions())
|
|
}
|
|
}
|