DataItem
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
DataItemas 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
DataItemwith a generic value that can be converted to a validInput.Declaration
Swift
convenience public init(converting value: DataInputConvertible) -
Wrap an
Encodableor a JSON serializable type of object into aDataItemby serializing it.You can safely pass to this method the result of deserializing a JSON object using
JSONSerializationor any subset of that JSON object. Prefer conforming your types toDataInputConvertiblerather thenEncodablefor Objects that need to be wrapped intoDataItemorDataObject, and callDataItem(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 validDataInput. Prefer using other methods when dealing with validDataInputwhose type are known at compile time (like if you have aStringor an array ofIntfor example).Warning
Non conforming floats like
Double.nanorFloat.infinitywill be silently converted to strings “NaN” and “Infinity” (or “-Infinity” for negative “Infinity”) immediately by this function.Throws
An
EncodingErrorif 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:
DoubleFloatIntInt64BoolStringNSNumber
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 followingNSNumberconversion 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
DataItemif 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 aDataItem.Declaration
Swift
public func getDataArray() -> [DataItem]? -
Returns the value as a Dictionary of
DataItemif 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 aDataItem.Declaration
Swift
public func getDataDictionary() -> [String : DataItem]? -
Returns the value as an
Arrayof the (optional) given type.This method will follow the same principles of the
get<T: DataInput>(as:)counterpart, but applies them on the individualArrayelements.Supported types are:
DoubleFloatIntInt64BoolStringNSNumber
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 followingNSNumberconversion 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
Dictionaryof the (optional) given type.This method will follow the same principles of the
get<T: DataInput>(as:)counterpart, but applies them on the individualDictionaryvalues.Supported types are:
DoubleFloatIntInt64BoolStringNSNumber
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 followingNSNumberconversion 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
View on GitHub