i'm new in swift and my question might be dummie for most of you. But any way i'm trying to learn by doing. Here is my problem. i have a model :
import Foundation
import Firebase
struct special {
let name: String
let position: Int
let imageURL: String?
}
class SpecialList {
var specialList: [special] = []
init() {
}
func loadSpecial () {
db.collection("special").getDocuments { (querySnapshot, error) in
if let e = error {
print("Error\(e)")
} else {
if let snapshotDocuments = querySnapshot?.documents {
for doc in snapshotDocuments {
let data = doc.data()
if let name = data["name"] as? String, let position = data["position"] as? Int, let imageURL = data["imageURL"] as? String {
let newList = special(name: name, position: position, imageURL: imageURL)
self.specialList.append(newList)
}
}
}
}
}
}
}
and i'm trying to implement that in ViewController:
var specialList = SpecialList()
override func viewDidLoad() {
specialList.loadSpecial()
print(specialList.specialList)
}
actually what i need is the data that that retrieves from firebase. I'm trying to save it in var specialList: [special] = []but it always empty. I think i should do something in init() but didnt found the way to do it right.
P.S. loading from firebase working fine. Checked with printing the data.
and the data should be in collectionView
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return specialList.specialList.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "SpecialCell", for: indexPath as IndexPath) as! SpecialCollectionViewCell
if let imageURL = specialList.specialList[indexPath.row].imageURL {
let url = URL(string: imageURL)
cell.specialPic.kf.setImage(with: url) // download foto
}
cell.specialName.text = specialList.specialList[indexPath.row].name
return cell
}