我試圖讓用戶通過搜索用戶名來開始遊戲並關注其他用戶。我需要能夠確保具有該用戶名的用戶存在。我使用下面的代碼,但雖然if
被稱爲else
不應該被調用時,它應該。Swift&Firebase |使用用戶名檢查用戶是否存在
let checkWaitingRef = Firebase(url:"https://test.firebaseio.com/users")
checkWaitingRef.queryOrderedByChild("username").queryEqualToValue("\(username!)")
.observeEventType(.ChildAdded, withBlock: { snapshot in
if snapshot.value.valueForKey("username")! as! String == username! {
} else {
}
JSON數據樹
{
"097ca4a4-563f-4867ghj0-6209288bd7f02" : {
"email" : "[email protected]",
"uid" : "097ca4a4-563f-4867ghj0-6209288bd7f02",
"username" : "test1",
"waiting" : "0"
},
"55a8f979-ad0d-438u989u69-aa4a-45adb16175e7" : {
"email" : "[email protected]",
"uid" : "55a8f979-ad0d-438u989u69-aa4a-45adb16175e7",
"username" : "test2",
"waiting" : "0"
}
}
「不過雖然如果叫別人不會被調用時,我想」你是什麼意思嗎? – Khuong
@ khuong291我的意思是if語句是「激活」:'如果snapshot.value.valueForKey(「username」)!如!字符串==用戶名! {「但即使應該這個else語句也不會做任何事情。 –
它是正確的,因爲如果語句是真的,那麼else語句永遠不會被執行。 – Khuong