이 데이터 구조가 있고 올바른 값을 추출 할 수 없습니다.
users
private
userID
birthday: "birthdayValue"
username: "nathan"
firstName: "Nathan"
etc...
내 앱에서 Firebase 실시간 데이터베이스를 통해 사용자 이름을 통해 사용자를 검색하는 검색 기능을 만들고 있습니다.
let reference = Database.database().reference()
if(searchText != ""){
reference.child("users").child("private").queryOrdered(byChild: "username").queryStarting(atValue: searchText).queryEnding(atValue: searchText + "\u{f8ff}").observeSingleEvent(of: .value, with: { (snapshot) in
if snapshot.value is NSNull{
//handles errors
return
}
else{
if let user = snapshot.value as? NSDictionary {
for child in user{
print(child.key)
print(child.value)
}
}
else{
//null
}
}
})
현재 두 개의 print 문은 검색 할 때마다 콘솔에 다음 두 결과를 인쇄합니다.
wnszfmHilqNl6PG9khWtWkKUPtF3
{
birthday = 100;
dateCreated = "1579543450313.94";
description = nil;
email = "[email protected]";
firstName = Nathan;
instagramLink = nil;
lastLogin = "1579543450313.988";
lastName = Ellis;
profilePicURL = "url";
twitchLink = nil;
username = nathan;
youtubeLink = nil;
}
예상대로 사용자 ID (키)와 스냅 샷 값을 NSDictonary로 인쇄합니다. 나는 사용자 이름을 얻는 데에만 관심이 있습니다. 내 검색 컨트롤러의 배열에 사용자 이름을 문자열로 추가 할 수 있도록이 Firebase 스냅 샷에서 사용자 이름을 추출하려면 어떻게해야합니까?
userID는 항상 다르기 때문에 분명히 동적이어야합니다.
데이터 모델을 변경해야합니까?
당신도 child.value
사전 인 것 같으므로 다음을 통해 액세스 할 수 있습니다.
if let valueDict = child.value as? [String: AnyObject] {
if let username = valueDict["username"] as? String {
// append username to results
print(username)
}
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다