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.
-
get(key:Extension methodas: ) Returns the data at the given key 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 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 followingNSNumberconversion 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 : DataInputParameters
keyThe key at which to look for the item.
typeThe 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.
-
getConvertible(key:Extension methodconverter: ) 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
keyThe key at which to look for the item.
converterThe 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
keyThe 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
keyThe 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:Extension methodof: ) Returns the value at the given key as an
Arrayof the (optional) given type.This method will follow the same principles of the
get<T: DataInput>(key: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 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 followingNSNumberconversion 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 : DataInputParameters
keyThe key at which to look for the array.
typeThe 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
nilif they were of the wrong type. -
getDictionary(key:Extension methodof: ) Returns the value at the given key as an
Dictionaryof the (optional) given type.This method will follow the same principles of the
get<T: DataInput>(key:as:)counterpart, but applies them on the individualDictionaryelements.Supported types are:
DoubleFloatIntInt64BoolStringNSNumber
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 followingNSNumberconversion 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 : DataInputParameters
keyThe key at which to look for the dictionary.
typeThe 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
nilif they were of the wrong type.
View on GitHub