【发布时间】:2016-03-02 00:11:34
【问题描述】:
这应该是超级基本的,但我还是遇到了错误。
Cannot subscript a value of type 'Dictionary<String, AnyObject>' with an index of type 'String'
这是我的代码:
func createComments(attributes: [[String: AnyObject]], votes: [String: AnyObject], sid: Int) -> [Comment] {
var comments: [Comment] = [Comment]()
for commentAttributes in attributes {
let comment = Comment()
comment.commentId = commentAttributes["id"]
comments.append(comment)
}
return comments
}
我在这一行收到错误:
comment.commentId = commentAttributes["id"]
据我所知,commentAttributes 应该是一个字典,其键为字符串,值为 AnyObject。除了使用字符串下标之外,我不确定如何使用字符串键访问字典的值。我在这里错过了什么?
【问题讨论】:
-
Comment的定义在哪里?它有成员commentId吗?
标签: swift dictionary subscript