1

I'm trying to better understand where would each have a distinct use case.

From what I understand:

  • array: is a simple optional array.
  • arrayValue: is a non-optional array that will return an empty array if it's nil

  • But I can't correctly understand where would you want to use arrayObject? It seems that you would use that when the entity is something other than JSON. But I can't understand why/how something could be other than JSON, is it about custom objects that we create ourselves?

This is the JSON extension related to arrays:

extension JSON {

    //Optional [JSON]
    public var array: [JSON]? {
        if self.type == .array {
            return self.rawArray.map { JSON($0) }
        } else {
            return nil
        }
    }

    //Non-optional [JSON]
    public var arrayValue: [JSON] {
        return self.array ?? []
    }

    //Optional [Any]
    public var arrayObject: [Any]? {
        get {
            switch self.type {
            case .array:
                return self.rawArray
            default:
                return nil
            }
        }
        set {
            if let array = newValue {
                self.object = array as Any
            } else {
                self.object = NSNull()
            }
        }
    }
}
mfaani
  • 33,269
  • 19
  • 164
  • 293
  • Most of the time you would just use arrayObject – Leo Dabus Oct 02 '17 at 19:34
  • @LeoDabus When you're in [Canada, the 80s don't come to Canda until til like 93](http://www.imdb.com/title/tt0885872/quotes/qt0453933). From the place I am at, we're just about to migrate from Swift 2.3 to Swift 3 (not 4) :(. So I would be pleased if you would still explain the differences. – mfaani Oct 02 '17 at 19:37
  • Can you elaborate why most of the time you would use arrayObject? The code base I was looking into, `arrayValue` was used 5X more... – mfaani Oct 02 '17 at 19:39
  • If you are just fetching values from an API you would just use arrayObject. I haven’t used SwiftyJSON since Swift 3. Read this https://developer.apple.com/swift/blog/?id=37 – Leo Dabus Oct 02 '17 at 19:46
  • You might be interested in this answer also https://stackoverflow.com/a/43121890/2303865 – Leo Dabus Oct 02 '17 at 19:50

0 Answers0