Swift 'subscript' 的使用不明确
Swift Ambiguous use of 'subscript'
我最近在我的项目中添加了新的 pods 并更新了以前的代码,这段代码之前都有效,现在我得到了 "Ambiguous use of 'subscript'" 我一直在检查其他答案,看起来我目前设置它的方式是正确的方式。我不确定为什么这些代码行会突然被标记为错误。
databaseRef.child("following").queryOrderedByKey().observeSingleEvent(of: .value, with: { (postsSnapshot) in
let posts = postsSnapshot.value as! [String: AnyObject]
for (_, post) in posts {
for (_, postInfo) in post as! [String: AnyObject] {
if let followingID = postInfo["uid"] as? String {
for each in self.following {
if each == followingID {
guard let uid = postInfo["uid"] as! String? else {return}
guard let name = postInfo["businessName"] as! String? else {return}
guard let address = postInfo["businessStreet"] as! String? else {return}
guard let state = postInfo["businessState"] as! String? else {return}
guard let city = postInfo["businessCity"] as! String? else {return}
let data = ["uid":postInfo["uid"] as! String, "businessName":postInfo["businessName"] as! String, "businessStreet":postInfo["businessStreet"] as! String, "businessState":postInfo["businessState"] as! String, "businessCity":postInfo["businessCity"] as! String,"businessZIP":postInfo["businessZIP"] as! String, "businessPhone":postInfo["businessPhone"] as! String, "businessLatitude":postInfo["businessLatitude"] as! String, "businessLongitude":postInfo["businessLongitude"] as! String, "businessWebsite":postInfo["businessWebsite"] as! String, "facebookURL":postInfo["facebookURL"] as! String, "foursquareURL":postInfo["foursquareURL"] as! String, "googleURL":postInfo["googleURL"] as! String, "instagramURL":postInfo["instagramURL"] as! String, "snapchatURL":postInfo["snapchatURL"] as! String, "twitterURL":postInfo["twitterURL"] as! String, "yelpURL":postInfo["yelpURL"] as! String]
问题出在您对 AnyObject
的使用上。 postInfo
被声明为 AnyObject
但你试图像字典一样使用它,因此出现错误。
如果post
是一个字典的字典,那么声明是这样的:
变化:
for (_, postInfo) in post as! [String: AnyObject] {
至:
for (_, postInfo) in post as! [String: [String: AnyObject]] {
这会将 postInfo
更改为 [String: AnyObject]
而不是 AnyObject
。
我最近在我的项目中添加了新的 pods 并更新了以前的代码,这段代码之前都有效,现在我得到了 "Ambiguous use of 'subscript'" 我一直在检查其他答案,看起来我目前设置它的方式是正确的方式。我不确定为什么这些代码行会突然被标记为错误。
databaseRef.child("following").queryOrderedByKey().observeSingleEvent(of: .value, with: { (postsSnapshot) in
let posts = postsSnapshot.value as! [String: AnyObject]
for (_, post) in posts {
for (_, postInfo) in post as! [String: AnyObject] {
if let followingID = postInfo["uid"] as? String {
for each in self.following {
if each == followingID {
guard let uid = postInfo["uid"] as! String? else {return}
guard let name = postInfo["businessName"] as! String? else {return}
guard let address = postInfo["businessStreet"] as! String? else {return}
guard let state = postInfo["businessState"] as! String? else {return}
guard let city = postInfo["businessCity"] as! String? else {return}
let data = ["uid":postInfo["uid"] as! String, "businessName":postInfo["businessName"] as! String, "businessStreet":postInfo["businessStreet"] as! String, "businessState":postInfo["businessState"] as! String, "businessCity":postInfo["businessCity"] as! String,"businessZIP":postInfo["businessZIP"] as! String, "businessPhone":postInfo["businessPhone"] as! String, "businessLatitude":postInfo["businessLatitude"] as! String, "businessLongitude":postInfo["businessLongitude"] as! String, "businessWebsite":postInfo["businessWebsite"] as! String, "facebookURL":postInfo["facebookURL"] as! String, "foursquareURL":postInfo["foursquareURL"] as! String, "googleURL":postInfo["googleURL"] as! String, "instagramURL":postInfo["instagramURL"] as! String, "snapchatURL":postInfo["snapchatURL"] as! String, "twitterURL":postInfo["twitterURL"] as! String, "yelpURL":postInfo["yelpURL"] as! String]
问题出在您对 AnyObject
的使用上。 postInfo
被声明为 AnyObject
但你试图像字典一样使用它,因此出现错误。
如果post
是一个字典的字典,那么声明是这样的:
变化:
for (_, postInfo) in post as! [String: AnyObject] {
至:
for (_, postInfo) in post as! [String: [String: AnyObject]] {
这会将 postInfo
更改为 [String: AnyObject]
而不是 AnyObject
。