Swift cast dictionary value as type

I am looking for the best way to make this code in one line:

if (dictionary["Amoumt"] is Double) {
    amount = dictionary["Amount"] as Double
} else {
    amount = NSString(string: dictionary["Amount"] as String).doubleValue
}

I have Dictionary<String, AnyObject>, and I would like to analyze the values ​​from it. I am using code as shown above, but too many lines. I would like to do this on one line. Sort of:

dictionary["Amount"].parseDouble()

There is no need to create an extension using this method:

func parseDouble() -> Double {
    if (self is Double) {
        return self as Double
    } else {
        return NSString(string:(self as String)).doubleValue
    }
}

But what type should I extend? Could you help me using the general method? Therefore, I could call something like this:

dictionary["Amount"].parse(Double)

And this is a good way how to do it or should I do it differently?

+4
source share
4 answers

You can use it AnyObjectas it is. Try:

var dictionary:[String:AnyObject] = [
    "foo": 4.21,    // NSNumber
    "bar": "42.5",  // NSString
    "baz": [1,2,3], // NSArray
]

let foo = dictionary["foo"]?.doubleValue ?? 0 // -> 4.21
let bar = dictionary["bar"]?.doubleValue ?? 0 // -> 42.5
let baz = dictionary["baz"]?.doubleValue ?? 0 // -> 0.0

, NSNumber NSString .doubleValue. , NSArray , nil.

:

Objective-C . Objective-C , @objc.

+1

, :

extension Dictionary{

    func parseDouble(key:Key) -> Double {

        let result = self[key]

        if (result is Double) {
            return result as Double
        } else {
            return NSString(string:(result as String)).doubleValue
        }
    }
}

, :

var doubleValue = dictionary.parseDouble("amount")

extension Dictionary{

    func parse<T>(key:Key) -> T? {

        let result = self[key]

        if (result is T) {
            return result as? T
        }

        return nil
    }
}

var doubleValue:Double? = myDistionary.parse("someKeyForDouble")

var stringValue:String? = myDistionary.parse("someKeyForString")
+1

- nil double. . , , , , `dict [amount] ' .

var amount: Double? = nil

let dict: [String: AnyObject] = ["amount" : "1.2"]

amount = (dict["amount"] as? Double) ?? NSString(string: dict["amount"] as! String).doubleValue

:

func getDouble(obj: AnyObject) -> Double {
    return (obj as? Double) ?? NSString(string: obj as! String).doubleValue
}

getDouble(dict["amount"]). - , , .

0

, AnyObject - AnyObject, .

AnyObject - , .

Dictionary - .

, - :

func getDouble(obj: AnyObject?) -> Double {
    return (obj as? Double) ?? (obj as? NSString)?.doubleValue ?? 0
}
asDouble(dictionary["Amount"])

. , , , . , as ! as?, , - - .

You can argue that it should return optional, with nil if the value was not convertible to double. This is what it does String.toInt(). Unfortunately, the limitation NSString.doubleValueis that it does not do this - by default it is zero, so you cannot combine these two approaches.

0
source

All Articles