2017-12-30 20 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는, 내가 객체를 만든 (사용자의 내 배열에 사전의 정보를 추가, 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에서 관련없는 유형 [문자열 : AnyObject]으로 Xcode 조건부 캐스트 경고가 항상 실패 함 – bibscy

관련 문제