简体   繁体   English

将AnyObject转换为String时,类型'String'不符合协议'NSCopying'错误

[英]Type 'String' does not conform to protocol 'NSCopying' error when downcast AnyObject to String

I'm tring to parse a JSON format like this: 我想要解析像这样的JSON格式:

{ 
    "key_1" : {
        "key_2" : "value"
    }

}

and then assign "value" to a variable. 然后将"value"赋给变量。

Here is my code: 这是我的代码:

var variableShouldBeAssigned: String
if let x = (jsonResult["key_1"]? as? NSDictionary) {
    if let y = (x["key_2"]? as? String) {
        variableShouldBeAssigned = y
    }
}

However, an error occurs when I try to downcast from x["key_2"]? 但是,当我尝试从x["key_2"]?向下x["key_2"]?时发生错误x["key_2"]? to a String, but it's fine to downcast from jsonResult["key_1"]? 到一个字符串,但从jsonResult["key_1"]?向下jsonResult["key_1"]?是好的jsonResult["key_1"]? to an NSDictionary. 到NSDictionary。

I can solve the error by using x["key_2"] to replace x["key_2"]? 我可以用x["key_2"]替换x["key_2"]?来解决错误x["key_2"]? , but I don't really know why it only works for jsonResult["key_1"]? ,但我真的不知道为什么它只适用于jsonResult["key_1"]? .

Can anybody tell me the reason? 谁能告诉我原因?

String does not conform to NSCopying, but surely NSString does! 字符串不符合NSCopying,但NSString确实如此! Also, going from NSString to String is instantaneously implied... 此外,从NSString到String瞬间暗示......

So I would say try something like this... Change String to NSString 所以我会说尝试这样的事情......将字符串更改为NSString

here is a sample, assuming that you handle the jsonResult as a NSDictionary... 这是一个示例,假设您将jsonResult作为NSDictionary处理...

func giveDictionary(jsonResult:NSDictionary) -> String?
{

    if let x = (jsonResult["key_1"]? as? NSDictionary)
    {
        if let y = (x["key_2"]? as? NSString)
        {
            return y
        }
    }
    return nil
}

You can simplify all your type checking by using a Swift dictionary at the beginning: 您可以在开头使用Swift字典简化所有类型检查:

var variableShouldBeAssigned: String
if let dict = jsonResult as? [String:[String:String]] {
    if let key1Dict = dict["key_1"] {
        if let value = key1Dict["key_2"] {
            variableShouldBeAssigned = value
        }
    }
}

In fact, you can even combine the two last if statements: 实际上,您甚至可以将最后两个if语句组合在一起:

var variableShouldBeAssigned: String
if let dict = jsonResult as? [String:[String:String]] {
    if let value = dict["key_1"]?["key_2"] {
       variableShouldBeAssigned = value
    }
}

In general, you should using Swift Dictionaries instead of NSDictionary 通常,您应该使用Swift Dictionaries而不是NSDictionary

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从NSDictionary获取对象时,类型'String'不符合协议'NSCopying'错误 - Type 'String' does not conform to protocol 'NSCopying' error when getting object from NSDictionary 类型[String:String]不符合协议“ AnyObject” - type [String: String] does not conform to protocol 'AnyObject' 类型“MyWeights”不符合协议“NSCopying” - Type 'MyWeights' does not conform to protocol 'NSCopying' 参数类型“ AnyObject”不符合预期的类型NSCopying - Argument Type 'AnyObject' does not conform to expected type NSCopying 字符串不符合NSarray Swift中的AnyObject类型 - String does not conform to type AnyObject in NSarray Swift 输入'[NSObject:AnyObject]!' 不符合协议'DictionaryLiteralConvertible' - Type '[NSObject : AnyObject]!' does not conform to protocol 'DictionaryLiteralConvertible' 类型MCSessionState不符合协议“ AnyObject” - Type MCSessionState does not conform to protocol 'AnyObject' 类型“ AnyObject”不符合协议“ sequenceType” - Type 'AnyObject' does not conform protocol 'sequenceType' 类型“T”不符合协议“AnyObject” - Type 'T' does not conform to protocol 'AnyObject' 类型'AnyObject'不符合协议'NSFetchRequestResult' - Type 'AnyObject' does not conform to protocol 'NSFetchRequestResult'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM