我刚开始使用Swift
进行编程,以便更好地学习语言,我正在创建一个类似于Tinder
的应用程序但是为了交朋友。我已完成注册过程,现在我正在努力让其他用户显示为用户将刷卡的卡片。为此,我想以某种方式创建一组符合条件的用户,以便我可以访问每个用户数据,但我似乎无法弄清楚如何执行此操作。不要犹豫,询问您是否需要进一步澄清。所有帮助表示赞赏!谢谢!
import UIKit
import Firebase
class CardsVC: UIViewController {
var membersInSchool = [User]()
var cardCount = 0
//@IBOutlet weak var cardView: CardView!
@IBOutlet weak var potentialMatchImage: UIImageView!
@IBOutlet weak var potentialMatchName: UILabel!
@IBOutlet weak var potentialMatchGrade: UILabel!
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func viewDidLoad() {
super.viewDidLoad()
fetchPotentialMatches()
//I want to implement completion handler here so setUpCard() only runs after fetchPotentialMatches() finishes
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0, execute: {
self.setUpCard()
})
}
@IBAction func skip(_ sender: Any) {
setUpCard()
}
@IBAction func like(_ sender: Any) {
setUpCard()
}
func fetchPotentialMatches(){
//Get the current user's information
let userRef = ref.child("users").child(uid!)
userRef.observe(.value, with: { (snapshot) in
let user = User(snapshot: snapshot)
print(user.highSchool!)
print(user.matchPreference!)
let currentUserSchool = user.highSchool!
let currentUserMatch = user.matchPreference!
//Get all the users uid's in the current user's school
ref.child("schools").child(currentUserSchool).child("members").observe(.value , with: { (snapshot) in
for child in snapshot.children {
let child = child as? DataSnapshot
if let otherUsers = child?.key {
//Get the information about each user in this school
ref.child("users").child(otherUsers).observe(.value, with: { (snap) in
let otherU = User(snapshot: snap)
//Check if user is not the current signed in user and the user matches the current user's match preference
if otherU.uid != uid! && otherU.gender == currentUserMatch {
self.membersInSchool.append(otherU)
print(otherU.firstName!)
print(otherU.lastName!)
let name = otherU.firstName! + " " + otherU.lastName!
}
})
}
}
})
}){(error) in
print(error.localizedDescription)
}
}
//Set up the card
func setUpCard(){
print(membersInSchool)
var potMatchName = membersInSchool[cardCount].firstName! + " " + membersInSchool[cardCount].lastName!
potentialMatchName.text! = potMatchName
var potMatchGrade = membersInSchool[cardCount].grade!
potentialMatchGrade.text! = potMatchGrade
self.cardCount += 1
}
}
当我尝试访问membersInSchool [1]时,我目前正在超出范围错误。我认为这是因为当我追加每个用户时,它不会在数组中创建一个新对象,而是将用户添加到membersInSchool [0]。
以下是我的User类的代码:
import Foundation
import UIKit
import Firebase
import FirebaseDatabase
struct User {
var firstName : String!
var lastName : String!
var gender : String!
var matchPreference : String!
var photoURL : String!
var highSchool : String!
var grade : String!
var uid : String!
var ref : DatabaseReference?
var key : String!
init(snapshot: DataSnapshot) {
key = snapshot.key
ref = snapshot.ref
firstName = (snapshot.value! as! NSDictionary)["first name"] as! String
lastName = (snapshot.value! as! NSDictionary)["last name"] as! String
gender = (snapshot.value! as! NSDictionary)["gender"] as! String
matchPreference = (snapshot.value! as! NSDictionary)["match preference"] as! String
photoURL = (snapshot.value! as! NSDictionary)["pic"] as? String
highSchool = (snapshot.value! as! NSDictionary)["School"] as! String
grade = (snapshot.value! as! NSDictionary)["grade"] as! String
uid = (snapshot.value! as! NSDictionary)["uid"] as! String
}
}