DataItem

final public class DataItem
extension DataItem: DataInputConvertible
extension DataItem: Decodable

A wrapper class that contains a generic JSON value.

You can use the utility getters to obtain the value in the correct type:

  • You can read any number as Int/Float/Double/NSNumber intercheangebly since it’s backed by an NSNumber on disk.
  • Arrays and Dictionaries will always contain DataItem as values and you can use specific getter to get the correct types from them too.

Warning

Do NOT cast this wrapper class to anything else as it will fail. Use the appropriate conversion methods instead.

Mistake example:

  let numbers: [Int?]? = DataItem(value: [1, 2, 3]).getDataArray() as? [Int?] // Cast will fail and numbers will be nil
  let numbers: [Int?]? = DataItem(value: [1, 2, 3]).getArray() // This will succeed
  let numbers: [Int?]? = DataItem(value: [1, 2, 3]).getDataArray().map { $0.getInt() } // This will also succeed
  let numbers: [Int]? = DataItem(value: [1, 2, 3]).getArray().compactMap { $0 } // This will also succeed and remove potentially nil values from the array
  • Initialize a DataItem with a specific value that is a valid DataInput.

    Declaration

    Swift

    convenience public init(value: DataInput)
  • Initialize a DataItem with a generic value that can be converted to a valid Input.

    Declaration

    Swift

    convenience public init(converting value: DataInputConvertible)
  • Wrap an Encodable or a JSON serializable type of object into a DataItem by serializing it.

    You can safely pass to this method the result of deserializing a JSON object using JSONSerialization or any subset of that JSON object. Prefer conforming your types to DataInputConvertible rather then Encodable for Objects that need to be wrapped into DataItem or DataObject, and call DataItem(converting: object) as that conversion is not failable.

    Additionally you can pass any valid DataInput, including any sort of Dictionary or Array that only contain a mix of valid DataInput. Prefer using other methods when dealing with valid DataInput whose type are known at compile time (like if you have a String or an array of Int for example).

    Warning

    Non conforming floats like Double.nan or Float.infinity will be silently converted to strings “NaN” and “Infinity” (or “-Infinity” for negative “Infinity”) immediately by this function.

    Throws

    An EncodingError if any other type of values are passed in the parameter or in eventual nested values.

    Declaration

    Swift

    public convenience init(serializing value: Any) throws
  • Declaration

    Swift

    public func getConvertible<T>(converter: any DataItemConverter<T>) -> T?

    Return Value

    The value after having been converted by the DataItemConverter.

  • Returns the data in the requested type if the conversion is possible.

    Supported types are:

    • Double
    • Float
    • Int
    • Int64
    • Bool
    • String
    • NSNumber

    You can call this method without parameters if the return type can be inferred:

     let dataItem: DataItem = ...
     let anInt: Int? = dataItem.get()
    

    Alternatively the type must be specified as a parameter:

     let dataItem: DataItem = ...
     let anInt = dataItem.get(as: Int.self)
    

    Every numeric type (Int, Int64, Float, Double, NSNumber) can be used interchangeably and the conversion will be made following NSNumber conversion methods.

     let nsNumber = NSNumber(1.5)
     let dataItem = DataItem(value: nsNumber)
     let aDouble: Double? = dataItem.get() // Double(1.5)
     let anInt: Int? = dataItem.get() // Int(1)
    

    Declaration

    Swift

    public func get<T>(as type: T.Type = T.self) -> T? where T : DataInput
  • Returns the value as an Array of DataItem if the underlying value is an Array.

    Warning

    Do not cast the return object as any cast will likely fail. Use the appropriate methods to extract value from a DataItem.

    Declaration

    Swift

    public func getDataArray() -> [DataItem]?
  • Returns the value as a Dictionary of DataItem if the underlying value is a Dictionary.

    Warning

    Do not cast the return object as any cast will likely fail. Use the appropriate methods to extract value from a DataItem.

    Declaration

    Swift

    public func getDataDictionary() -> [String : DataItem]?
  • Returns the value as an Array of the (optional) given type.

    This method will follow the same principles of the get<T: DataInput>(as:) counterpart, but applies them on the individual Array elements.

    Supported types are:

    • Double
    • Float
    • Int
    • Int64
    • Bool
    • String
    • NSNumber

    You can call this method without parameters if the return type can be inferred:

     let dataItem: DataItem = ...
     let anIntArray: [Int?]? = dataItem.getArray()
    

    Alternatively the type must be specified as a parameter:

     let dataItem: DataItem = ...
     let anIntArray = dataItem.getArray(of: Int.self)
    

    Every numeric type (Int, Int64, Float, Double, NSNumber) can be used interchangeably and the conversion will be made following NSNumber conversion methods.

     let nsNumber = NSNumber(1.5)
     let dataItem = DataItem(value: [nsNumber])
     let aDoubleArray = dataItem.getArray(of: Double.self) // [Double(1.5)]
     let anIntArrat = dataItem.getArray(of: Int.self) // [Int(1)]
    

    Declaration

    Swift

    public func getArray<T>(of type: T.Type = T.self) -> [T?]? where T : DataInput
  • Returns the value as a Dictionary of the (optional) given type.

    This method will follow the same principles of the get<T: DataInput>(as:) counterpart, but applies them on the individual Dictionary values.

    Supported types are:

    • Double
    • Float
    • Int
    • Int64
    • Bool
    • String
    • NSNumber

    You can call this method without parameters if the return type can be inferred:

     let dataItem: DataItem = ...
     let anIntDictionary: [Int?]? = dataItem.getDictionary()
    

    Alternatively the type must be specified as a parameter:

     let dataItem: DataItem = ...
     let anIntDictionary = dataItem.getDictionary(of: Int.self)
    

    Every numeric type (Int, Int64, Float, Double, NSNumber) can be used interchangeably and the conversion will be made following NSNumber conversion methods.

     let nsNumber = NSNumber(1.5)
     let dataItem = DataItem(value: ["key": nsNumber])
     let aDoubleDictionary = dataItem.getDictionary(of: Double.self) // ["key": Double(1.5)]
     let anIntDictionary = dataItem.getDictionary(of: Int.self) // ["key": Int(1)]
    

    Declaration

    Swift

    public func getDictionary<T>(of type: T.Type = T.self) -> [String : T?]? where T : DataInput
  • Declaration

    Swift

    public func toDataInput() -> DataInput
  • Declaration

    Swift

    convenience public init(from decoder: Decoder) throws