I am trying to create a simple Firebase application, and I have previously done so on Android. I am learning swift and was following the basic tutorials provided by Firebase.
I have successfully added Firebase through CocoaPods, however I am having trouble reading the result:
import UIKit
import Firebase
class ViewController: UIViewController {
@IBOutlet var mLabel: UILabel!
var myRootRef = Firebase(url:"https://ios-weather-practice.firebaseio.com")
override func viewDidLoad() {
super.viewDidLoad()
myRootRef.observeEventType(.Value, withBlock: {
snapshot in
if let baseResult = snapshot.value.objectForKey("ios-weather-practice") {
self.mLabel.text = baseResult as? String
}
})
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Error:
2016-04-14 16:37:06.424 Firebase Sample[1833:1735025] - [NSTaggedPointerString objectForKey:]: unrecognized selector sent to instance 0xa000000747365744
2016-04-14 16:37:06.432 Firebase Sample[1833:1735025] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSTaggedPointerString objectForKey:]: unrecognized selector sent to instance 0xa000000747365744'
*** First throw call stack:
(
0 CoreFoundation 0x00000001066f5f45 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x0000000108419deb objc_exception_throw + 48
2 CoreFoundation 0x00000001066fe56d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3 CoreFoundation 0x000000010664beea ___forwarding___ + 970
4 CoreFoundation 0x000000010664ba98 _CF_forwarding_prep_0 + 120
5 Firebase Sample 0x0000000106367759 _TFFC15Firebase_Sample14ViewController11viewDidLoadFS0_FT_T_U_FGSQCSo13FDataSnapshot_T_ + 329
6 Firebase Sample 0x00000001063679f7 _TTRXFo_oGSQCSo13FDataSnapshot__dT__XFo_iGSQS___iT__ + 23
7 Firebase Sample 0x0000000106366d81 _TPA__TTRXFo_oGSQCSo13FDataSnapshot__dT__XFo_iGSQS___iT__ + 81
8 Firebase Sample 0x0000000106367a30 _TTRXFo_iGSQCSo13FDataSnapshot__iT__XFo_oGSQS___dT__ + 32
9 Firebase Sample 0x0000000106367a78 _TTRXFo_oGSQCSo13FDataSnapshot__dT__XFdCb_dGSQS___dT__ + 56
10 Firebase 0x00000001064316e1 __43-[FValueEventRegistration fireEvent:queue:]_block_invoke53 + 78
11 libdispatch.dylib 0x000000010980ae5d _dispatch_call_block_and_release + 12
12 libdispatch.dylib 0x000000010982b49b _dispatch_client_callout + 8
13 libdispatch.dylib 0x00000001098132af _dispatch_main_queue_callback_4CF + 1738
14 CoreFoundation 0x00000001066562e9 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
15 CoreFoundation 0x00000001066178a9 __CFRunLoopRun + 2073
16 CoreFoundation 0x0000000106616e08 CFRunLoopRunSpecific + 488
17 GraphicsServices 0x000000010ace9ad2 GSEventRunModal + 161
18 UIKit 0x0000000106f1230d UIApplicationMain + 171
19 Firebase Sample 0x000000010636840d main + 109
20 libdyld.dylib 0x000000010985f92d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
Firebase Database:
Observing by .Value will read in the entire contents of a node.
So if your structure is like this
myRootRef
iso-weather-practice: "sunny"
Your event will be ok and it could be read like this
self.mLabel.text = baseResult as? String
However, that's not usually how a Firebase structure would appear. It would be more like this
myRootRef
-JYa6s6gij900is
date: "20160413"
weather: "sunny"
-JY9398iias98o4
date: "20160414"
weather: "blowing snow"
and in that case. .Value will read in that entire node, and the snapshot will contain multiple nodes (as key:value pairs)
So here's how to handle it:
ref.observeEventType(.Value, withBlock: { snapshot in
for child in snapshot.children {
if let w = child.value.objectForKey("weather") as? String {
print(w)
}
}
})
output will be
sunny
blowing snow