我有一个数组,该数组将始终具有4个不同的autoId(它从firebase的节点上随机获取4个)-它是图像中显示的randomKeyArray:
我想使用randomKeyArray中的autoID从firebase(用户全名和profileImageUrl)获取更多信息,并将它们设置在单独的数组(名称,profileImages和namesWithUrl)中,以便以后使用。
我希望randomKeyArray [0]对应于名称[0],profileImages [0],namesWithUrl [0],并与1,2,3等对应。
现在,名称,profileImages和namesWithUrl数组为空,我不确定为什么-希望得到任何帮助:)
// the arrays are stored outside the viewDidLoad
var randomKeyArray = [String]()
var names = [String]()
var profileImages = [String]()
var namesWithUrl = [String : String]()
//EDIT: this for loop is where i get the random autoId's for the myKeyArray
for _ in 0...3 { //will iterate four times
let count = self.myKeyArray.count //get the number of elements
let randomInt = Int.random(in: 0..<count) //get a random index for the array
let randomUserKey = self.myKeyArray[randomInt]
self.randomKeyArray.append(randomUserKey)
self.myKeyArray.remove(at: randomInt) //remove that object so it's not selected again
}
print(self.myKeyArray)
//for loop is inside a function
for i in 0..<self.randomKeyArray.count {
let thisUserKey = self.randomKeyArray[i]
self.ref.child("users").child(thisUserKey).observeSingleEvent(of: .value, with: { snapshot in
let name = snapshot.childSnapshot(forPath: "fullname").value as! String
let profileImageUrl = snapshot.childSnapshot(forPath: "profileImageUrl").value as! String
self.names.append(name)
self.profileImages.append(profileImageUrl)
self.namesWithUrl[name] = profileImageUrl
//self.currIds.append(self.randomKeyArray)
})
}
让我知道是否需要更多信息!
编辑:
我添加了for循环,我获得了randomKeyArray的autoId-我还尝试在第一个for循环中调用randomUserKey时获取名称和配置文件图像,但是数组仍然显示0个值