2017-12-30 41 views
0
func fetchUser() { 
    Database.database().reference().child("users").observe(.childAdded, with: { (DataSnapshot) in 
     if let dictionary = DataSnapshot.value as? [String: AnyObject] { 
      let user = User() 
      user.setValuesForKeys(dictionary.) 
      print(user.name!, user.email!) 

     } 
    }, withCancel: nil) 

} 

用信号SIGABRT私のアプリに自分のfirebaseデータベースからのデータを追加するユーザーの私のアレイへの辞書からの情報を追加するとき、それはSIGABRTエラーがスローされます(私は、オブジェクトを作成しましたために)。私は何をしているのですか?スウィフトエラー - スレッド1:setValuesForKeys

ログにエラーメッセージは、この氏は述べています:

Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<ChatApp.User 0x10bdeec70> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key name.' 
*** First throw call stack: 
(0x18136a364 0x1805b0528 0x18136a02c 0x181c82630 0x181d1e7dc 0x100016170 0x10001645c 0x10016dd7c 0x100199524 0x1012a92cc 0x1012a928c 0x1012adea0 0x181312544 0x181310120 0x18122fe58 0x1830dcf84 0x18a8af67c 0x100025fbc 0x180d4c56c) 
libc++abi.dylib: terminating with uncaught exception of type NSException 
+1

例外の理由をよく読んでください。おそらくこのクラスはキーXYZ *のコード値に対応したキー値ではありません。 XYZはクラス内のキーで、辞書と一致しません。ところで、SwiftのKVCのsetValuesForKeysより優れた方法があります。 – vadian

答えて

0

データベース内のあなたの鍵は、あなたのユーザーオブジェクトクラス内の変数名と一致しない場合に、この問題が発生しました。これを避けるには、次のように手動で手動で行うことができます。

let reference = Database.database().reference() 
    reference.child("users").observe(.childAdded, with: { (snapshot) in 

     guard let dictionary = snapshot.value as? [String: AnyObject] else { return } 
     guard let username = dictionary["username"] as? String else { return } 

     let user = User() 
     user.username = username 
     // do this for everything the user object needs to hold. 

    }, withCancel: nil) 
+0

FFIRDataSnapshotから無関係の型[String:AnyObject]へのXcode条件付きキャストの警告が常に失敗する – bibscy

関連する問題