我正在寻找创建一个函数,将允许我拉出更多的朋友列表基于我当前的朋友列表。我从提取我当前的朋友列表开始,对于每个朋友,我提取他们的朋友列表并将其附加到一个数组中。但是,我现在的输出类似于"friendId1","friendId2",我希望它只显示第二行"friendId1","friendId2“,"friendId3","friendId4”
func fetchOtherFriends(completion: @escaping ([String]) -> Void) {
let currentUser = Auth.auth().currentUser!.uid
Database.database().reference().child("friends").child(currentUser).observeSingleEvent(of: .value, with: { (snapshot : DataSnapshot) in
// Iterate through all the children:
for child in snapshot.children.allObjects as! [DataSnapshot] {
let value = child.value as? NSDictionary
let userId = value?["userId"] as? String ?? ""
Database.database().reference().child("friends").child(userId).observeSingleEvent(of: .value, with: { (snapshot : DataSnapshot) in
// Iterate through all the children:
for child1 in snapshot.children.allObjects as! [DataSnapshot] {
let value1 = child1.value as? NSDictionary
let userId1 = value1?["userId"] as? String ?? ""
self.otherfriendsArray.append(userId1)
completion(self.otherfriendsArray)
}
})
}
})
}发布于 2017-12-11 00:59:19
如果我正确理解了您的问题,那么您可以使用flatmap。
输入:
let friendList = [["friend1","friend2"],["friend3","friend4"]]解决方案:
let result = friendList.flatMap{ $0 }输出:
["friend1", "friend2", "friend3", "friend4"]https://stackoverflow.com/questions/47740605
复制相似问题