I am trying to populate a collection view. When I go to use a value in a dictionary, it is showing up as nil, but when I print it the line before it provides me with a string.
I fixed this issue before, but when I remade my collection view to solve another bug I was having, this bug came back. To fix it, I just removed the line self.users.removeAll(). Adding that line back in then removing it doesn't work this time.
I am having problems specifically in this function:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionview.dequeueReusableCell(withReuseIdentifier: "userCell", for: indexPath) as! UserCell
print(user[indexPath.row].imagePath!)
cell.userImage.sd_setImage(with: URL(string: user[indexPath.row].imagePath!))
cell.nameLabel.text = user[indexPath.row].username
cell.userID = user[indexPath.row].userID
return cell
}
Here is the full code:
import UIKit
import Firebase
import SwiftKeychainWrapper
import SwiftUI
import FirebaseUI
class UserViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
@IBOutlet weak var collectionview: UICollectionView!
var user = [User]()
override func viewDidLoad() {
super.viewDidLoad()
collectionview.delegate = self
collectionview.dataSource = self
retrieveUsers()
}
func retrieveUsers() {
let ref = Database.database().reference()
ref.child("users/").observeSingleEvent(of:.value, with: { (snapshot) in
let users = snapshot.value as! [String : NSDictionary]
//self.user.removeAll()
for (_, value) in users {
if let uid = value["uid"] as? String {
if uid != Auth.auth().currentUser!.uid {
let userToShow = User()
if let username = value["username"] as? String, let imagePath = value["urlToImage"] as? String {
userToShow.username = username
userToShow.imagePath = imagePath
userToShow.userID = uid
self.user.append(userToShow)
print(userToShow)
}
}
}
}
self.collectionview.reloadData()
})
ref.removeAllObservers()
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionview.dequeueReusableCell(withReuseIdentifier: "userCell", for: indexPath) as! UserCell
print(user[indexPath.row].imagePath!)
cell.userImage.sd_setImage(with: URL(string: user[indexPath.row].imagePath!))
cell.nameLabel.text = user[indexPath.row].username
cell.userID = user[indexPath.row].userID
return cell
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return user.count //?? 0
}
func checkFollowing(indexPath: IndexPath) {
let uid = Auth.auth().currentUser!.uid
let ref = Database.database().reference()
ref.child("users").child(uid).child("following").queryOrderedByKey().observeSingleEvent(of: .value, with: { snapshot in
if let following = snapshot.value as? [String : AnyObject] {
for (_, value) in following {
if value as! String == self.user[indexPath.row].userID {
// self.tableview.cellForRow(at: indexPath)?.accessoryType = .checkmark
}
}
}
})
ref.removeAllObservers()
}
@IBAction func logOutPressed(_ sender: Any) {
KeychainWrapper.standard.removeObject(forKey:"uid")
do {
try Auth.auth().signOut()
} catch let signOutError as NSError{
print("Error signing out: %@", signOutError)
}
dismiss(animated: true, completion: nil)
}
}
extension UIImageView {
func downloadImage(from imgURL: String!) {
let url = URLRequest(url: URL(string: imgURL)!)
let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
if error != nil {
print(error!)
return
}
DispatchQueue.main.async {
self.image = UIImage(data: data!)
}
}
task.resume()
}
}
Am I not populating the dictionary correctly?
Also, here is the User class:
import UIKit
class User: NSObject {
var userID: String?
var username: String?
var imagePath: String?
}
Also, this is what the user dictionary shows in the debugger: