Reputation: 71
i want to fix this problem Cannot call value of non-function type 'Any'
this is my code
databaseRef.child("ServiceA").queryOrderedByKey().observe(.childAdded, with: {
DataSnapshot in
let title = DataSnapshot.value!("title") as! String
self.posts.insert(PostStruct.init(title: title), at: 0)
})
Is there a way to fix it?
Thanks
Upvotes: 0
Views: 506
Reputation: 459
DataSnapshot.value is Any
and according to the documentation you could cast it in different types.
Data types returned: + NSDictionary + NSArray + NSNumber (also includes booleans) + NSString
If I understood your snipped well, in you case, you need to cast "DataSnapshot" into a dictionary in order to access with a subscript. You can try this:
databaseRef.child("ServiceA").queryOrderedByKey().observe(.childAdded, with: {
DataSnapshot in
let title = (snapshot.value as? [String: Any])?["title"] as? String
self.posts.insert(PostStruct.init(title: title), at: 0)
})
Please also note that, in order to prevent crashes, I removed the force-wrap so "title" would be an optional String.
Upvotes: 1