字符串不可转换为DictionaryIndex< String,Any> [英] String is not Convertible to DictionaryIndex<String, Any>
问题描述
我一直在使用[String:AnyObject]字典直到我遇到无法将生成的AnyObject下转到Swift字符串,这在技术上不是一个对象,而是一个值类型。我可以和平,因为它似乎是有道理的,虽然如果有办法(除了恢复NSString对象,我不想做),我想知道。
上面的逻辑解决方案似乎是切换到使用 [String:Any]
字典,但是在这样做时,我找到了一个即使使用基本代码:
func abc(){
var dict:[String:Any] = [String:Any]()
如果dict [success]!= nil {
...
}
}
上面的第三行代码出现错误:
'String'不能转换为'DictionaryIndex< String,Any>'。
现在我似乎有一个包含类型的字典我怀疑我可以根据需要进行倒塌,但是这个钥匙不再可以看到,据我所知,这些键是完全正确的(确实,尽管它是亵渎,甚至错误表明我正在传递一个String)。我所缺少的是,所有我实际想要实现的是具有易于访问的多种类型的值的字典,我可以在正确的Swift类型的var / let中实际检索到
这里的问题似乎是您使用任何
。 字典
有两个下标方法:
subscript(i:DictionaryIndex< Key ,Value>) - > (Key,Value){get}
下标(key:Key) - >值?
编译器错误地假设你想要第一个,然后抱怨说您传入的字符串
不能转换为 DictionaryIndex< Key,Value>
。
为了避免这个问题,您可以:
-
强制使用特定的下标访问器:
-
将您的字典声明为
[String:AnyObject]
而不是 - 因为元组不是对象。从指南,
-
AnyObject
可以表示任何类型的实例。
-
任何
可以表示一个实例除了功能类型之外,任何类型的文件都是任何类型的。
-
如果您认为这是错误,您可能希望提交错误。
I'm working with dictionaries in Swift which need to hold differing types of value, although the keys are pretty much all strings. This was painfully easy with Objective-C, but Swift's generally very useful type safety seems to make this more difficult.
I had been working with [String: AnyObject] dictionaries until I ran into being unable to downcast a resulting AnyObject to a Swift string, which technically isn't an object but a value type. I can make peace with this as it seems to make sense, although if there's a way around it (besides reverting to NSString objects which I don't want to do), i'd like to know it.
The logical solution to the above seems to be switching to use [String: Any]
dictionaries, but in doing so I'm finding a problem even with a basic piece of code:
func abc() {
var dict : [String: Any] = [String: Any]()
if dict["success"] != nil {
...
}
}
The 3rd line of code above gives the error:
'String' is not convertible to 'DictionaryIndex<String, Any>'.
Now I seem to have a dictionary that contains types that I suspect I can downcast as I need to, but that no longer seems accessible by its keys, which as far as I can see are of exactly the right type (indeed, although it's a literal, even the error suggests I am passing a String). What am I missing, given all I'm actually trying to achieve is dictionaries with easily accessible values of multiple types which I can actually retrieve in a var/let of the correct Swift type?
The issue here seems to be your use of Any
. Dictionary
has two subscript methods:
subscript (i: DictionaryIndex<Key, Value>) -> (Key, Value) { get }
subscript (key: Key) -> Value?
The compiler incorrectly assumes you want the first one, and then complains that the String
you passed in can't be converted to DictionaryIndex<Key, Value>
.
To avoid this problem, you can either:
Force it to use a specific subscript accessor:
if dict["success"] as Any? != nil ...
Declare your dictionary as
[String:AnyObject]
instead — since tuples aren't objects. From the guide,AnyObject
can represent an instance of any class type.Any
can represent an instance of any type at all, apart from function types.
If you believe this is an error, you may wish to file a bug.
这篇关于字符串不可转换为DictionaryIndex< String,Any>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!