无法为类型“ AnyObject?”调用初始化程序没有参数Swift 4.2

时间:2019-04-20 04:19:37

标签: swift firebase

var loggedInUser = AnyObject?() //Cannot invoke initializer for type 'AnyObject?' with no arguments

不知道为什么它会给我这个我不熟悉的错误

var databaseRef = Database.database().reference()
var storageRef = Storage.storage().reference()

// structure definition goes here
override func viewDidLoad() {
    super.viewDidLoad()

    self.loggedInUser = Auth.auth()?.currentUser
    self.databaseRef.child("user_profiles").child(self.loggedInUser!.uid).observeSingleEventOfType(.Value) { (snapshot:DataSnapshot) in

var databaseRef = Database.database().reference()
var storageRef = Storage.storage().reference()

// structure definition goes here
override func viewDidLoad() {
    super.viewDidLoad()

    self.loggedInUser = Auth.auth()?.currentUser

    self.databaseRef.child("user_profiles").child(self.loggedInUser!.uid).observeSingleEventOfType(.Value) { (snapshot:DataSnapshot) in

2 个答案:

答案 0 :(得分:1)

Firbase类型为User,因此将其声明为

var loggedInUser: User?

答案 1 :(得分:0)

无法以您的方式初始化AnyObject。您可以声明为可选变量

以这种方式声明:

var loggedInUser : AnyObject?