简体   繁体   中英

Firebase Syntax with Swift 3.0

Here is some Firebase related code taken from an iOS app of mine:

var dataRef = firebaseRootReference?.child(byAppendingPath: "MyList")
dataRef = dataRef?.child(byAppendingPath: key)

dataRef.observe(.value, with: { snapshot in
    .......
    }, withCancel: { error in
        print(error.description)
})

The above code was working fine. But I just upgraded my project to Swift 3.0 and I now get this error message:

Cannot convert value of type '(_) -> ()' to expected argument type '((Error?) -> Void)!'

Browsing the net I can only find examples of similar code without error handling part. If I just comment out one part like below, I get rid of the error but I doubt this is the right solution.

var dataRef = firebaseRootReference?.child(byAppendingPath: "MyList")
dataRef = dataRef?.child(byAppendingPath: key)

dataRef?.observe(.value, with: { snapshot in
    .......
    }/*, withCancel: { error in
        print(error.description)
}*/)

What is the right way to write the code above in Swift 3.0 ?

You are getting this error because of line print(error.description) . Error protocol doesn't have property description , simply changed it with localizedDescription will solve your problem.

print(error.localizedDescription)

The whole code will be like below.

dataRef?.observe(.value, with: { snapshot in

    //get data from snapshot object
}, withCancel: { error in

    print(error.localizedDescription)
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM