Ios 无法调用非函数类型的值';有吗';:-快速3号火力基地
这是我转到Swift 3之前的代码:Ios 无法调用非函数类型的值';有吗';:-快速3号火力基地,ios,swift,firebase,firebase-realtime-database,Ios,Swift,Firebase,Firebase Realtime Database,这是我转到Swift 3之前的代码: ref.observeEventType(.ChildAdded, withBlock: { snapshot in let currentData = snapshot.value!.objectForKey("Dogs") if currentData != nil { let mylat = (currentData!["latitude"])! as! [String]
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)
}
})
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)
}
})
这是我转到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)
}
})
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)
}
})
然后我在第二行得到一个错误:
无法调用非函数类型“Any?!”的值
我唯一尝试的是将第二行更改为以下代码:
snapshot.value as! [String:AnyObject]
但这是不对的,没有“狗”包括在内,它给了我一个警告,即从未使用过
位置之间的距离代码。看到的问题是,当您实例化和初始化变量时,您告诉它,它将收到的值将是本文档中名为Dogs
的对象的值类型为AnyObject
的快照
但是snapshot.value
属于Dictionary类型,即[String:AnyObject]
,NSDictionary
您检索的Dogs
节点属于字典或数组类型
基本上,您应该避免将值存储在任何对象类型的变量中
试试这个:-
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:-看到你的JSON结构,你可能想把它转换成字典,而不是Swift 3中的数组AnyObject
已被更改为Any
,它实际上可以是任何东西。
特别是在使用键和索引订阅时,现在需要告诉编译器实际的类型
解决方案是将snapshot.value
强制转换为Swift字典类型[String:Any]
。可选绑定安全地打开值
if let snapshotValue = snapshot.value as? [String:Any],
let currentData = snapshotValue["Dogs"] as? [String:Any] {
let mylat = currentData["latitude"] as! [String]
...
您使用的感叹号太多。纬度“]
后面的标记在as!
之前不需要,如果让而不是检查nil我还有一个代码段,允许您访问子节点的值。我希望这对您有所帮助:
if let snapDict = snapShot.value as? [String:AnyObject] {
for child in snapDict{
if let name = child.value as? [String:AnyObject]{
var _name = name["locationName"]
print(_name)
}
}
}
致以最良好的祝愿,
Nazar Medeiros我在if行中得到一个警告:比较“[String:AnyObject]类型的非可选值“to nil总是返回true
以及我在问题中写的DistanceBetween Wolocations
中的警告。我已经更新了我的答案。至于DistanceBetween Wolocations,我无法说话,因为我看不到你的代码中写了什么。现在它通过当前数据(在我删除child(“Posts”)之后)。child(“post1”)
。但是在distance-between-wolocations
中仍然会收到一条警告。出于某种原因,我在移动到Swift 3之前没有遇到这个问题。什么是distance-between-wolocations
您在哪里声明的。没有更多信息,无法帮助您。。是的,它帮助了我,而且它是正确的!非常感谢!显然distance-between-wolocation由于Firebase身份验证连接,ions
无法工作。如果您想提供帮助,我提出了一个新问题:)此处:到目前为止还不错,但在DistanceBetweenWolocations
行代码中,我收到一条警告:调用“DistanceBetweenWolocations”(\uUUU2:destination:userid:)'未使用
。正如它警告我的那样,由于某种原因,它从未使用过(在更改为Swift 3之前使用过)。调用方法而不使用结果是毫无意义的。您之前的Swift 3代码也不使用结果。在Swift 3中,您会收到更多关于未使用结果的警告,您可以将@discardableResult
放在函数前面,或者如果结果根本没有使用,则删除结果类型。没门!它工作得很好!It显示了位置之间的所有用户,在更改为swift 3后,什么都没有发生。什么是@discardableResult
?是获取所有传递的currentData
的可选绑定?@discardableResult
抑制未使用的结果
警告。