简体   繁体   中英

Swift Firebase “Cannot assign value of type 'Information' to type 'NSDictionary?'”

I have a tableview that is being populated with who a user is following. Problem is that I need to pass that cells data to "var otherUser: NSDictionary!" but because I am populating the cell using a data structure file called "Information" I get this error - "Cannot assign value of type 'Information' to type 'NSDictionary?'" in the prepareForSegue. I am unsure if I can repackage the information I need into a NSDictionary so I can successfully do a data pass. I just don't know if this is a easy solution or an actual problem because of my ignorance.

Following TableViewController Code

import UIKit
import Firebase

class BusinessFollowing: UITableViewController {

@IBOutlet var noDataView: UIView!
@IBOutlet var followingTableView: UITableView!

var yourFollowing = [Information]()

var listFollowing = [NSDictionary?]()
var databaseRef = Database.database().reference()
let uid = Auth.auth().currentUser?.uid


var loggedInUser = Auth.auth().currentUser
var loggedInUserData:NSDictionary?

var following = [String]()

override func viewDidLoad() {
    super.viewDidLoad()

    self.followingTableView.backgroundView = nil


}

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    self.followingTableView.reloadData()
    self.yourFollowing.removeAll()
    self.following.removeAll()

    getFollowingData()

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    super.prepare(for: segue, sender: sender)
    if segue.identifier == "following" {
        // gotta check if we're currently searching
            if let indexPath = followingTableView.indexPathForSelectedRow {
                let user = self.yourFollowing[indexPath.row]
                let controller = segue.destination as? ExploreBusinessProfileSwitchView
                controller?.otherUser = user
            }
    }
}

override func numberOfSections(in tableView: UITableView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows
    return self.yourFollowing.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! BusinessFollowingCell
    let following = yourFollowing[indexPath.row]

    let businessName = following.businessName
    let businessStreet = following.businessStreet
    let businessCity = following.businessCity
    let businessState = following.businessState

    cell.businessName.text = businessName
    cell.businessStreet.text = businessStreet
    cell.businessCity.text = businessCity
    cell.businessState.text = businessState

    // cell.businessName?.text = self.listFollowing[indexPath.row]?["businessName"] as? String
    // cell.businessStreet?.text = self.listFollowing[indexPath.row]?["businessStreet"] as? String
    // cell.businessCity?.text = self.listFollowing[indexPath.row]?["businessCity"] as? String
    // cell.businessState?.text = self.listFollowing[indexPath.row]?["businessState"] as? String

    return cell
}

func getFollowingData() {
    self.yourFollowing.removeAll()
    self.following.removeAll()
    self.followingTableView.reloadData()

    Database.database().reference().child("Businesses").child((loggedInUser?.uid)!).child("following").observe(.value, with: { snapshot in
        if snapshot.exists() {
            MBProgressHUD.showAdded(to: self.view, animated: true)
            let databaseRef = Database.database().reference()
            databaseRef.child("Businesses").queryOrderedByKey().observeSingleEvent(of: .value, with: { (usersSnapshot) in
                let users = usersSnapshot.value as! [String: AnyObject]
                for (_, value) in users {
                    if let userID = value["uid"] as? String {
                        if userID == Auth.auth().currentUser?.uid {
                            print(value)
                            if let followingUsers = value["following"] as? [String : String] {
                                for (_,user) in followingUsers {
                                    self.following.append(user)
                                }
                            }
                            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}

                                                    self.yourFollowing.append(Information(uid: uid, businessName: name, businessStreet: address, businessCity: city, businessState: state))
                                                }

                                                self.followingTableView.backgroundView = nil
                                                self.followingTableView.reloadData()
                                            }
                                        }
                                    }
                                }
                                MBProgressHUD.hide(for: self.view, animated: true)
                            }) { (error) in
                                print(error.localizedDescription)
                            }
                        }
                    }
                }

            })
        } else {
            print("Not following anyone")
            self.followingTableView.backgroundView = self.noDataView
            MBProgressHUD.hide(for: self.view, animated: true)
        }
    })

}

}

"Information" Data Structure File

import UIKit

class Information {

var uid: String
var businessName: String
var businessStreet: String
var businessCity: String
var businessState: String


init(uid: String, businessName: String, businessStreet: String, businessCity: String, businessState: String){

    self.uid = uid
    self.businessName = businessName
    self.businessStreet = businessStreet
    self.businessCity = businessCity
    self.businessState = businessState
}

}

The error is pretty clear.

user in ExploreBusinessProfileSwitchView is obviously declared as NSDictionary , declare it as Information .

By the way don't use NSArray / NSDictionary in Swift. Use native types.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM