SwiftyJson 的初步理解

SwiftyJson 的初步理解

  1. 最初的认识 SwiftyJson 的本质就是一个 Public Struct JSON结构体

  2. Struct JSON 的构成

     public Struct JSon {
         fileprivate var rawArray: [Any] = []
         fileprivate var rawDictionary: [String: Any] = [:]
         fileprivate var rawString: String = ""
         fileprivate var rawNumber: NSNumber = 0
         fileprivate var rawNull: NSNull = NSNull()
         fileprivate var rawBool: Bool = false

         /// JSON type, fileprivate setter
         public fileprivate(set) var type: Type = .null

         /// Error in JSON, fileprivate setter
         public fileprivate(set) var error: SwiftyJSONError?
         public static var nullJSON: JSON { return null }
         public static var null: JSON { return JSON(NSNull()) }

         /// Object in JSON   这个是核心的属性 所有的操作就是 在这给个地方开始的
         public var object: Any {
             get {
                     switch self.type {
                         case .array:
                                 return self.rawArray
                         case .dictionary:
                                 return self.rawDictionary
                         case .string:
                                 return self.rawString
                         case .number:
                                 return self.rawNumber
                         case .bool:
                                 return self.rawBool
                         default:
                                 return self.rawNull
                         }
                 }
             set {
                     error = nil
                     switch unwrap(newValue) {
                         case let number as NSNumber:
                                 if number.isBool {
                                         type = .bool
                                         self.rawBool = number.boolValue
                                     } else {
                                         type = .number
                                         self.rawNumber = number
                                     }
                         case let string as String:
                                 type = .string
                                 self.rawString = string
                         case _ as NSNull:
                                 type = .null
                         case nil:
                                 type = .null
                         case let array as [Any]:
                                 type = .array
                                 self.rawArray = array
                         case let dictionary as [String: Any]:
                                 type = .dictionary
                                 self.rawDictionary = dictionary
                         default:
                                 type = .unknown
                                 error = SwiftyJSONError.unsupportedType
                         }
                 }
     }

3 - Set 我的初步认识 没有对其他的重载方法分析 只对整体的做个大致的介绍 初始化方法的时候 设置object 属性的值 在开始初始化的时候 最先开始的是 初始化懒加载的属性 然后再对 object 属性 做set 赋值 赋值的时候。实现了unwrap(newValue)这个递归的方法 推导出json的所有基础类型 并且返回的是JSon初始化后的结构体 大概是这样的 我最喜欢的就是unwrap(newValue) 递归的设置思路 很佩服作者

JSON{JSON{JSoN } }

一个一个的JSON结构体组成

4 Get值的时候 对下标进行了重载 可传递多个参数的 和 传递数组【】

可传递多个参数的重载 
public subscript(path: JSONSubscriptType...) -> JSON {
             get {
                     return self[path]
                 }
             set {
                     self[path] = newValue
                 }
         }
传递数组的重载
public subscript(path: [JSONSubscriptType]) -> JSON {
             get {
                     return path.reduce(self) { $0[sub: $1] }
                 }
             set {
                     switch path.count {
                         case 0:
                                 return
                         case 1:
                                 self[sub:path[0]].object = newValue.object
                         default:
                                 var aPath = path
                                 aPath.remove(at: 0)
                                 var nextJSON = self[sub: path[0]]
                                 nextJSON[aPath] = newValue
                                 self[sub: path[0]] = nextJSON
                         }
                 }
         }
传递 一个参数的重载 这个参数是 需要实现  JSONSubscriptType  
本上我们都只是会用到  string  和 Int  作者已经帮我们把对 String
和 Int  做了扩展 
public enum JSONKey {
         case index(Int)
         case key(String)
     }

 public protocol JSONSubscriptType {
         var jsonKey: JSONKey { get }
     }

 extension Int: JSONSubscriptType {
         public var jsonKey: JSONKey {
                 return JSONKey.index(self)
             }
     }

 extension String: JSONSubscriptType {
         public var jsonKey: JSONKey {
                 return JSONKey.key(self)
             }
     }
// 传递 一个参数的重载
fileprivate subscript(sub sub: JSONSubscriptType) -> JSON {
             get {
                     switch sub.jsonKey {
                         case .index(let index): return self[index: index]
                         case .key(let key): return self[key: key]
                         }
                 }
             set {
                     switch sub.jsonKey {
                         case .index(let index): self[index: index] = newValue
                         case .key(let key): self[key: key] = newValue
                         }
                 }
         }
  1. 还需了解的一个是 reduce高阶函数 如果你用过 map,filter,等 那么对reduce的理解也不是难 只需要理解的是 在执行的时候 每次返回的都是一个JSON的结构体 那么你就知道了为什么可以无限的使用下标获取到最后的值了

你可能感兴趣的:(iOS,Swift,结构,struct,json,SwiftyJson)