2017-07-07 8 views
0

firebaseを使用して2つの認証レベルでiOSアプリケーションを実行しようとしています。 2番目の認証は、データベースにコード魔法使いが置かれているテキストフィールドに挿入して検証する必要があります。firebaseを使用してスワイプコードに検索フィールドを作成する方法は?

json root

// 
// AccessoTerzoLivello.swift 
// amesci 
// 
// Created by Gianluca Caliendo on 07/07/17. 
// Copyright © 2017 Amesci. All rights reserved. 
// 

import UIKit 
import Firebase 

class AccessoTerzoLivello: UIViewController { 

    @IBOutlet weak var CodiceVolontarioTxt: UITextField! 

    @IBAction func Accedi(_ sender: UIButton) { 

    var rootRef: DatabaseReference! 

    rootRef = Database.database().reference() 

    var conditionalRef = rootRef.child("SchedaVolontari") 

    conditionalRef.observe(.value) {(snap: DataSnapshot) in } 

    if self.CodiceVolontarioTxt.text = snap 

    } 
    else { 
     let alert = UIAlertController(title: "Errore", message: "Codice volontario non valido", preferredStyle: UIAlertControllerStyle.alert) 
     alert.addAction(UIAlertAction(title: "ok", style: UIAlertActionStyle.default, handler: nil)) 

     self.present(alert, animated: true, completion: nil) 
    } 
    } 
} 
+0

ようこそStackOverflow。 SOの[how-to-use](https://stackoverflow.com/help/how-to-ask)セクションをチェックし、質問を更新してください。これまでに試したことの詳細も記入してください。 –

答えて

0

あなたは正しい道にあります。お使いの回線に問題があります:

if self.CodiceVolontarioTxt.text = snap

Snapがあなたのコードを保持している辞書ですが、あなたは、テキスト入力にそれを比較することはできません。まず、すべてのコードを取得する必要があります。

ここにいくつかのコードがあります。私はこれをテストしていないが、それはあなたにアイデアを与えるでしょう。補完ブロックのif else文を次のように置き換えます。

// This is empty array which will contain all the codes 
var codesArray: NSMutableArray = [] 

// Get all the children from snapshot you got back from Firebase 
let snapshotChildren = snap.children 

// Loop over all children (code) in Firebase 
while let child = snapshotChildren.nextObject() as? FIRDataSnapshot { 
    // Get code node key and save it to codes array 
    codes.add(child.key) 
} 

// Compare if inserted text matches any code from database 
if codesArray.contains(self.CodiceVolontarioTxt.text) { 
    print("Code exists in Firebase") 
} else { 
    print("Code does not exist in Firebase") 
} 
+0

助けてくれてありがとうございます。ZassXのコードには2つの問題があります:NSMutableArray型の値にメンバー 'append'がありません –

+0

@GianlucaCaliendo申し訳ありません、私の悪い、codesArray.add(child.getKey())を試してみてください。私は上記の答えを更新しました。 – ZassX

+0

まだ問題: "タイプ 'FIRDataSnapshot'の値にメンバー 'getKey'がありません" –

関連する問題