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 thanJSON
. But I can't understand why/how something could be other thanJSON
, 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()
}
}
}
}