[英]Extracting values from NSDictionary to individual variables
I really thought this was Xcode playing up as I can't for the life of me see what is causing this error in a fairly simple piece of Swift code but after restarting cleaning the project, restarting Xcode, and restarting my MacBook I'm still getting the same error.... But why!!??!! 我真的以为这是Xcode播放,因为我不能在我的生活中看到是什么导致这个错误在一个相当简单的Swift代码,但重新启动清理项目,重新启动Xcode,并重新启动我的MacBook我仍然得到同样的错误....但为什么!! ?? !!
Here is the code 这是代码
// Loop through records saving them
for recordItem in records {
if let record: NSDictionary = recordItem as? NSDictionary {
let time: String = record["time"] as! String
let record: String = record["record"] as! String
let recordId: String = record["record_id"] as! String
let saveTime: String = record["save_time"] as! String
let setId: String = record["set_id"] as! String
// Does more stuff
}
}
records is an NSArray and it is made from data downloaded from a remote server. records是一个NSArray,它是从远程服务器下载的数据制作的。
All is fine until the line beginning let recordId. 一切都很好,直到开始的行让recordId。 This and the two lines below it are giving an error - Cannot subscript a value of type 'String' with an index of type 'String'
这和它下面的两行给出了一个错误 - 不能下标一个'String'类型的值,其索引类型为'String'
Why? 为什么? And why is there no problem with the 2 lines above?
为什么上面的2行没有问题? And how do I fix it?
我该如何解决?
Any help is greatly appreciated as I'm at a bit of a dead end. 非常感谢任何帮助,因为我有点死路一条。
You are using the variable record
to unwrap the optional recordItem
but then you are re-assigning record
as record["record"]
- so from this point on it becomes a String, not a Dictionary - resulting in the error message Cannot subscript a value of type 'String' with an index of type 'String' 您正在使用变量
record
来解包可选的recordItem
但之后您将record
重新分配为record["record"]
- 所以从这一点开始它就变成了一个字符串,而不是一个字典 - 导致错误消息无法下标值'String'类型的索引类型为'String'
You just need to change one of the record
variables 您只需要更改其中一个
record
变量
for recordItem in records {
if let unwrappedRecord: NSDictionary = recordItem as? NSDictionary {
let time: String = unwrappedRecord["time"] as! String
let record: String = unwrappedRecord["record"] as! String
let recordId: String = unwrappedRecord["record_id"] as! String
let saveTime: String = unwrappedRecord["save_time"] as! String
let setId: String = unwrappedRecord["set_id"] as! String
// Does more stuff
}
}
The problem is here, you are declaring record
as a String
, previously it was NSDictionary
and due to scope the record
in record["record_id"]
is String
not NSDictionary
. 问题就在这里,你正在申报
record
作为一个String
,以前是NSDictionary
,并由于范围record
在record["record_id"]
是String
不是NSDictionary
。 Quick fix is to change the name as I did 快速解决方法是像我一样更改名称
let time: String = record["time"] as! String
let record1: String = record["record"] as! String
let recordId: String = record["record_id"] as! String
let saveTime: String = record["save_time"] as! String
let setId: String = record["set_id"] as! String
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.