ERROR:
错误:
Cannot subscript a value of type '[NSObject : AnyObject]?' with an index of type 'String'
不能为'[NSObject: AnyObject]类型的值加上下标?'具有'String'类型的索引'
CODE:
代码:
func getApple(appleId: String) {
var apples = userDefaults.dictionaryForKey("apples_array")
println(apples[appleId])
2 个解决方案
#1
13
Should be:
应该是:
var apples = userDefaults.dictionaryForKey("apples_array")
println(apples?[appleId])
The issue here is that type [NSObject : AnyObject]?
implies an optional type, which means you're attempting to call a subscript on what is essentially an enum. When you try to do this, there's no subscript declared, so the system chokes.
这里的问题是类型[NSObject: AnyObject]?表示一个可选类型,这意味着您试图在实质上是枚举的内容上调用下标。当您尝试这样做时,没有声明下标,所以系统会阻塞。
By adding the ?
we're saying, unwrap this value if possible, and then call the subscript. This way the system infers to look on type [NSObject : AnyObject]
for subscript declarations and everything is ok.
通过添加?我们说,如果可能的话打开这个值,然后调用下标。通过这种方式,系统推断要查找类型[NSObject: AnyObject]的下标声明,一切正常。
You could also use !
to force an unwrap, but this will crash if apples
is nil. Another possible way to write this would be:
你也可以用!要打开包装,但如果苹果为零,就会崩溃。另一种可能的写法是:
let apples = userDefaults.dictionaryForKey("apples_array") ?? [:]
println(apples[appleId])
This way, apples is no longer optional and it will always have the subscript syntax. No unwrapping necessary.
这样,apple就不再是可选的,它将始终具有下标语法。没有必要展开。
#2
4
I think it is far more clear to use optional binding so that the println is only invoked when there is an actual value to print
我认为使用可选绑定要清楚得多,只有在有实际值要打印时才调用println
func getApple(appleId: String) {
if let apples = userDefaults.dictionaryForKey("apples_array") {
println(apples[appleId])
}
}
#1
13
Should be:
应该是:
var apples = userDefaults.dictionaryForKey("apples_array")
println(apples?[appleId])
The issue here is that type [NSObject : AnyObject]?
implies an optional type, which means you're attempting to call a subscript on what is essentially an enum. When you try to do this, there's no subscript declared, so the system chokes.
这里的问题是类型[NSObject: AnyObject]?表示一个可选类型,这意味着您试图在实质上是枚举的内容上调用下标。当您尝试这样做时,没有声明下标,所以系统会阻塞。
By adding the ?
we're saying, unwrap this value if possible, and then call the subscript. This way the system infers to look on type [NSObject : AnyObject]
for subscript declarations and everything is ok.
通过添加?我们说,如果可能的话打开这个值,然后调用下标。通过这种方式,系统推断要查找类型[NSObject: AnyObject]的下标声明,一切正常。
You could also use !
to force an unwrap, but this will crash if apples
is nil. Another possible way to write this would be:
你也可以用!要打开包装,但如果苹果为零,就会崩溃。另一种可能的写法是:
let apples = userDefaults.dictionaryForKey("apples_array") ?? [:]
println(apples[appleId])
This way, apples is no longer optional and it will always have the subscript syntax. No unwrapping necessary.
这样,apple就不再是可选的,它将始终具有下标语法。没有必要展开。
#2
4
I think it is far more clear to use optional binding so that the println is only invoked when there is an actual value to print
我认为使用可选绑定要清楚得多,只有在有实际值要打印时才调用println
func getApple(appleId: String) {
if let apples = userDefaults.dictionaryForKey("apples_array") {
println(apples[appleId])
}
}