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()
            }
        }
    }
}
5
  • Most of the time you would just use arrayObject Commented Oct 2, 2017 at 19:34
  • @LeoDabus When you're in Canada, the 80s don't come to Canda until til like 93. 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. Commented Oct 2, 2017 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... Commented Oct 2, 2017 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 developer.apple.com/swift/blog/?id=37 Commented Oct 2, 2017 at 19:46
  • You might be interested in this answer also stackoverflow.com/a/43121890/2303865 Commented Oct 2, 2017 at 19:50

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.