如何将字典扩展为Dictionary <string,anyobject =“”>

时间:2015-10-23 12:18:21

标签: swift dictionary casting swift-extensions

我正在尝试将扩展中的字典解包为Dictionary。 但我遇到了一些问题,无法在任何地方找到答案。

我有一个获取字典的函数,其中我想从JSON字典中获取json字符串,当然如果它是一个有效的JSON对象:

// My Function
func foo(dict: Dictionary<String, AnyObject>) {
    let jsonString = dict.jsonString    // <-- 'String' is not a subtype of 'NSObject'
}

// My Extension
extension Dictionary where Key: NSObject, Value: AnyObject {
    var jsonString: String? {
        if let dict = self as? Dictionary<String, AnyObject> {
            do {
                let data = try NSJSONSerialization.dataWithJSONObject(self as Dictionary, options: NSJSONWritingOptions.PrettyPrinted)
                if let string = String(data: data, encoding: NSUTF8StringEncoding) {
                    return string
                }
            } catch {
                print(error)
            }
            return nil
        }
    }
}

但问题是我有var dict: Dictionary<String, AnyObject>

我也尝试过使用StringLiteralConvertible

extension Dictionary where Key: StringLiteralConvertible, Value: AnyObject {
    var jsonString: String? {
        if let dict = self as? Dictionary<String, AnyObject> {    // <-- 'Dictionary<Key, Value>' is not convertible to 'Dictionary<String, AnyObject>'
            do {
                let data = try NSJSONSerialization.dataWithJSONObject(self as Dictionary, options: NSJSONWritingOptions.PrettyPrinted)    // <-- Cannot convert value of type 'Dictionary<Key, Value>' to type 'Dictionary<_, _>' in coercion
                if let string = String(data: data, encoding: NSUTF8StringEncoding) {
                    return string
                }
            } catch {
                print(error)
            }
            return nil
        }
    }
}

但后来我遇到两个错误。 我怎样才能使它发挥作用?

2 个答案:

答案 0 :(得分:1)

我从您的问题中了解到,当您的词典键为String时,您想要添加一些功能(执行其他工作),且值为AnyObject

您可以使用扩展名(与问题中完全相同)

extension Dictionary where Key: String, Value: AnyObject {

// do what ever you want, you have the self object here
}

使用self,你可以做任何事情

更新后:

您可以将NSObject更改为String。这是我机器的截图:

check the real test usage in my project

更新2

这对你有好处吗?

extension Dictionary {
    var jsonString: String? {

        for (key, value) in self{
            if let isKey = key as? String {
                // do whatever you want
                return "Forza Roma"
            }else {
                return nil
            }

        }
        return "";
    }
}

答案 1 :(得分:0)

我找到了这样的答案,经过测试并且有效。如果有人知道这种方法的其他问题,那将会很有趣。 我已将self转换为AnyObject,然后转换为Dictionary<String, AnyObject>。接缝是可以接受的。

extension Dictionary where Key: StringLiteralConvertible, Value: AnyObject {
    var jsonString: String? {
        if let dict = (self as? AnyObject) as? Dictionary<String, AnyObject> {
            do {
                let data = try NSJSONSerialization.dataWithJSONObject(dict, options: NSJSONWritingOptions.PrettyPrinted)
                if let string = String(data: data, encoding: NSUTF8StringEncoding) {
                    return string
                }
            } catch {
                print(error)
            }
        }
        return nil
    }
}

如果你做(self as? NSObject) as? Dictionary<String, AnyObject>,它也会有用。

您最有可能删除转化为Disctionary<String, AnyObject>,如下所示:

extension Dictionary where Key: StringLiteralConvertible, Value: AnyObject {
    var jsonString: String? {
        do {
            let data = try NSJSONSerialization.dataWithJSONObject(self as! AnyObject, options: NSJSONWritingOptions.PrettyPrinted)
            if let string = String(data: data, encoding: NSUTF8StringEncoding) {
                return string
            }
        } catch {
            print(error)
        }
        return nil
    }
}