不能调用非函数类型的值“任何?!” : - Firebase,Swift3 [英] Cannot call value of non-function type 'Any?!' :- Firebase,Swift3

查看:179
本文介绍了不能调用非函数类型的值“任何?!” : - Firebase,Swift3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我之前的代码移动到Swift 3:
$ b

  ref.observeEventType(.ChildAdded ,withBlock:{
中的快照让currentData = snapshot.value!.objectForKey(Dogs)
if currentData!= nil {
let mylat =(currentData![latitude]) !as![String]
let mylat2 = Double((mylat [0]))
let mylon =(currentData![longitude])!as![String]
let mylon2 = double((mylon [0]))
let userid =(currentData![User])!as![String]
let userid2 = userid [0]
let otherloc = CLLocation(latitude:mylat2!,longitude:mylon2!)
self.distanceBetweenTwoLocations(self.currentLocation,destination:otherloc,userid:userid2)
}
})

这是我的代码在移动到Swift 3之后



  ref.observe(.childAdded,with:{snapshot in 
let currentData =(snapshot.value!作为AnyObject).object(Dogs)
if currentData!= nil {
let mylat =(currentData![latitude])!如! [String]
let mylat2 = Double((mylat [0]))
let mylon =(currentData![longitude])!如! [String]
let mylon2 = Double((mylon [0]))
let userid =(currentData![User])!如! [String]
let userid2 = userid [0]
let otherloc = CLLocation(latitude:mylat2!,longitude:mylon2!)
self.distanceBetweenTwoLocations(self.currentLocation,destination:otherloc,userid :userid2)
}
})

然后我得到一个错误第二行:
$ b


不能调用非函数类型的值Any'!'



我试过的唯一方法就是将第二行改为这段代码:

  snapshot.value as! [String:AnyObject] 

但是不对,没有狗警告: distanceBetweenTwoLocations 代码从来没有使用过。 解决方案

是当你实例化和初始化你的变量时,你告诉它,它的值将是一个 c $ c $> code $ c $> $ / code>> / code> snapshot.value 的类型是字典ie [String:AnyObject] NSDictionary ..



您检索的 Dogs 节点是类型,字典或数组。
$ b基本上你应该避免在一个变量中存储一个类型为AnyObject的值

FIRDatabase.database() observe(.childAdded,with:{snapshot in $ b $ if if currentData =(snapshot.value!).reference()。child(Posts)。如! NSDictionary).object(forKey:狗)为? [String:AnyObject] {

let mylat =(currentData [latitude])!如! [String]
let mylat2 = Double((mylat [0]))
let mylon =(currentData [longitude])!如! [String]
let mylon2 = Double((mylon [0]))
let userid =(currentData [User])!如! [String]
let userid2 = userid [0]
let otherloc = CLLocation(latitude:mylat2!,longitude:mylon2!)
self.distanceBetweenTwoLocations(self.currentLocation,destination:otherloc,userid :userid2)
}
})

PS: / strong>看到你的JSON结构,你可能想把它转换成Dictionary而不是Array


This is my code before moving to Swift 3:

ref.observeEventType(.ChildAdded, withBlock: { snapshot in
            let currentData = snapshot.value!.objectForKey("Dogs")
            if currentData != nil {
            let mylat = (currentData!["latitude"])! as! [String]
            let mylat2 = Double((mylat[0]))
            let mylon = (currentData!["longitude"])! as! [String]
            let mylon2 = Double((mylon[0]))
            let userid = (currentData!["User"])! as! [String]
            let userid2 = userid[0]
            let otherloc = CLLocation(latitude: mylat2!, longitude: mylon2!)
            self.distanceBetweenTwoLocations(self.currentLocation, destination: otherloc, userid: userid2)
            }
        })

This is my code after moving to Swift 3:

ref.observe(.childAdded, with: { snapshot in
            let currentData = (snapshot.value! as AnyObject).object("Dogs")
            if currentData != nil {
                let mylat = (currentData!["latitude"])! as! [String]
                let mylat2 = Double((mylat[0]))
                let mylon = (currentData!["longitude"])! as! [String]
                let mylon2 = Double((mylon[0]))
                let userid = (currentData!["User"])! as! [String]
                let userid2 = userid[0]
                let otherloc = CLLocation(latitude: mylat2!, longitude: mylon2!)
                self.distanceBetweenTwoLocations(self.currentLocation, destination: otherloc, userid: userid2)
            }
        })

And then I get an error in the 2nd line:

Cannot call value of non-function type 'Any?!'

The only thing I tried is changing the 2nd line to this code:

snapshot.value as! [String:AnyObject]

But it's not right, there is no "Dogs" included and it's giving me a warning that the distanceBetweenTwoLocations code never used.

解决方案

See the issue is that when you are instancing and initialising your variable you are telling it that the value its gonna receive will be a value for the object named Dogs present in this snapshot whose type is AnyObject.

But snapshot.value is of type Dictionary i.e [String:AnyObject],NSDictionary..

And the Dogs node that you retrieve is of type, either Dictionary or an Array.

Basically you should avoid to store a value in a variable, of type AnyObject

Try this:-

      FIRDatabase.database().reference().child("Posts").child("post1").observe(.childAdded, with: { snapshot in
        if let currentData = (snapshot.value! as! NSDictionary).object(forKey: "Dogs") as? [String:AnyObject]{

            let mylat = (currentData["latitude"])! as! [String]
            let mylat2 = Double((mylat[0]))
            let mylon = (currentData["longitude"])! as! [String]
            let mylon2 = Double((mylon[0]))
            let userid = (currentData["User"])! as! [String]
            let userid2 = userid[0]
            let otherloc = CLLocation(latitude: mylat2!, longitude: mylon2!)
            self.distanceBetweenTwoLocations(self.currentLocation, destination: otherloc, userid: userid2)
        }
    })

PS:- Seeing your JSON structure you might wanna convert it into a Dictionary not a Array

这篇关于不能调用非函数类型的值“任何?!” : - Firebase,Swift3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆