DataItemExtractor

public protocol DataItemExtractor : JSONPathExtractable where Self.Root == ObjectRoot

A container of key-value pairs that can return a DataItem for a given String key.

  • Returns the DataItem at the given key, if it was there; otherwise nil.

    Declaration

    Swift

    func getDataItem(key: String) -> DataItem?

    Parameters

    key

    The key at which to look for the DataItem.

    Return Value

    A DataItem if one can be found for the given key.

  • get(key:as:) Extension method

    Returns the data at the given key 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 dataExtractor: DataItemExtractor = ...
     let anInt: Int? = dataExtractor.get(key: "someKey")
    

    Alternatively the type must be specified as a parameter:

     let dataExtractor: DataItemExtractor = ...
     let anInt = dataExtractor.get(key: "someKey", 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 dataExtractor: DataItemExtractor = DataObject(dictionary: ["someKey": nsNumber])
     let aDouble: Double? = dataExtractor.get(key: "someKey") // Double(1.5)
     let anInt: Int? = dataExtractor.get(key: "someKey") // Int(1)
    

    Declaration

    Swift

    func get<T>(key: String, as type: T.Type = T.self) -> T? where T : DataInput

    Parameters

    key

    The key at which to look for the item.

    type

    The expected type of the item.

    Return Value

    The value at the given key if it was found and if it was of the correct type.

  • Gets and converts the item at the given key using the given converter.

    Declaration

    Swift

    func getConvertible<T>(key: String, converter: any DataItemConverter<T>) -> T?

    Parameters

    key

    The key at which to look for the item.

    converter

    The converter used to convert the item to the expected type.

    Return Value

    The value at the given key after having been converted by the DataItemConverter.

  • getDataArray(key:) Extension method

    Returns the value as an Array of DataItems 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

    func getDataArray(key: String) -> [DataItem]?

    Parameters

    key

    The key at which to look for the array.

    Return Value

    The array of DataItems, if an array is found at the given key.

  • getDataDictionary(key:) Extension method

    Returns the value as a Dictionary of DataItems 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

    func getDataDictionary(key: String) -> [String : DataItem]?

    Parameters

    key

    The key at which to look for the dictionary.

    Return Value

    The dictionary of DataItems, if a dictionary is found at the given key.

  • getArray(key:of:) Extension method

    Returns the value at the given key as an Array of the (optional) given type.

    This method will follow the same principles of the get<T: DataInput>(key: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 dataExtractor: DataItemExtractor = ...
     let anIntArray: [Int?]? = dataExtractor.getArray(key: "someKey")
    

    Alternatively the type must be specified as a parameter:

     let dataExtractor: DataItemExtractor = ...
     let anIntArray = dataExtractor.getArray(key: "someKey", 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 dataExtractor: DataItemExtractor = DataObject(dictionary: ["someKey": [nsNumber]])
     let aDoubleArray = dataExtractor.getArray(key: "someKey", of: Double.self) // [Double(1.5)]
     let anIntArray = dataExtractor.getArray(key: "someKey", of: Int.self) // [Int(1)]
    

    Declaration

    Swift

    func getArray<T>(key: String, of type: T.Type = T.self) -> [T?]? where T : DataInput

    Parameters

    key

    The key at which to look for the array.

    type

    The expected type of each item of the array.

    Return Value

    The array of items of optional type at the given key if it was found. Each item will be nil if they were of the wrong type.

  • getDictionary(key:of:) Extension method

    Returns the value at the given key as an Dictionary of the (optional) given type.

    This method will follow the same principles of the get<T: DataInput>(key:as:) counterpart, but applies them on the individual Dictionary 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 dataExtractor: DataItemExtractor = ...
     let anIntDictionary: [String: Int?]? = dataExtractor.getDictionary(key: "someKey")
    

    Alternatively the type must be specified as a parameter:

     let dataExtractor: DataItemExtractor = ...
     let anIntDictionary = dataExtractor.getDictionary(key: "someKey", 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 dataExtractor: DataItemExtractor = DataObject(dictionary: ["someKey": nsNumber])
     let aDoubleDictionary = dataExtractor.getDictionary(key: "someKey", of: Double.self) // ["someKey": Double(1.5)]
     let anIntDictionary = dataExtractor.getDictionary(key: "someKey", of: Int.self) // ["someKey": Int(1)]
    

    Declaration

    Swift

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

    Parameters

    key

    The key at which to look for the dictionary.

    type

    The expected type of each item of the dictionary.

    Return Value

    The dictionary of items of optional type at the given key if it was found. Each item will be nil if they were of the wrong type.