-1

I had recently added new pods to my project and updated the previous ones, this code had all worked before and now I am getting "Ambiguous use of 'subscript'" I have been checking other answers and it seems like the way I have it currently set up is the correct way. I am not sure why all of a sudden these lines of code would get flagged as errors.

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]
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Lukas Bimba
  • 817
  • 14
  • 35
  • 1
    Maybe unrelated but the syntax `as! String?` (force unwrap an optional to an optional) is nonsense. In a `guard` expression you are going to conditionally downcast the value so write `as? String`. And later use the unwrapped values: `let data = ["uid":uid, "businessName": name, ...` – vadian Nov 17 '18 at 17:27
  • ok thanks for that – Lukas Bimba Nov 17 '18 at 17:28
  • Still doesn't solve the problem. – Lukas Bimba Nov 17 '18 at 17:30

1 Answers1

1

The problem is your use of AnyObject. postInfo is being declared as AnyObject but then you attempt to use it like a dictionary, hence the error.

If post is a dictionary of dictionaries, then declare is as such:

Change:

for (_, postInfo) in post as! [String: AnyObject] {

to:

for (_, postInfo) in post as! [String: [String: AnyObject]] {

This will change postInfo to be [String: AnyObject] instead of AnyObject.

rmaddy
  • 314,917
  • 42
  • 532
  • 579