如何解决表达式类型不明确的错误?

时间:2019-02-02 14:41:55

标签: swift firebase

在此代码块中, “ let remoteUser = try User(记录:postDict,上下文:DatabaseManager.shared.persistentContainer.newBackgroundContext()) ” 行生成的错误类型是模棱两可的,没有更多上下文错误。

我有点不确定为什么会产生此错误-但是,我确实知道我有两个用户对象-firebase User类和应用程序本地的User类。当我将User指定为我的modulename.User时,会出现相同的错误。当我删除此用户逻辑时,应用程序将正确构建。

    Auth.auth().signIn(withEmail: emailAddress, password: password) { (user, error) in
        // If Successful, pull firstName/lastName
        let ref: DatabaseReference!
        ref = Database.database().reference().child("users").child(emailAddress)

        ref!.observe(DataEventType.value, with: { (snapshot) in
            // Get user value
            let postDict = snapshot.value as? NSDictionary ?? [:]
            let firstName = postDict["firstName"] as? String ?? ""
            let lastName = postDict["lastName"] as? String ?? ""
            let showroom = postDict["showroom"] as! NSDictionary
            let showroomReference = showroom["showroomID"] as? String ?? ""

            if showroomReference == "" {
                completionHandler(.failure(Error.unknownShowroom))
            }

            let remoteUser = try User(record: postDict, context: DatabaseManager.shared.persistentContainer.newBackgroundContext())

            self.fetchShowroom(forIdentifier: showroomReference) { (result) in
                do
                {
                    let showroom = try result.value()
                    remoteUser.showroom = showroom
                    remoteUser.brands = showroom.brands

                    completionHandler(.success(remoteUser))
                }
                catch
                {
                    completionHandler(.failure(error))
                }
            }
        })
    }

我将不胜感激-谢谢!除非我解决此错误,否则XCode不会生成。

1 个答案:

答案 0 :(得分:0)

该错误有点误导。 do - catch语句周围的try块丢失。

do {
   let remoteUser = try User(record: postDict, context: DatabaseManager.shared.persistentContainer.newBackgroundContext()
   self.fetchShowroom(forIdentifier: showroomReference) { (result) in
      do {
         let showroom = try result.value()
         remoteUser.showroom = showroom
         remoteUser.brands = showroom.brands
         completionHandler(.success(remoteUser))
      } catch {
         completionHandler(.failure(error))
      }
  }
} catch {
   completionHandler(.failure(error))
}