无法使用 Swift 读取 Firebase 数据
Can't Read Firebase Data with Swift
我正在尝试创建一个简单的 Firebase 应用程序,我之前已经在 Android 上这样做过。我正在学习 swift 并且正在学习 Firebase 提供的基本教程。
我已经通过 CocoaPods 成功添加了 Firebase,但是我无法读取结果:
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.
}
}
错误:
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 数据库:
试试这个:
if let baseResult = snapshot.value as? String {
self.mLabel.text = baseResult
}
通过 .Value 观察将读取节点的全部内容。
所以如果你的结构是这样的
myRootRef
iso-weather-practice: "sunny"
你的活动没问题,可以这样读
self.mLabel.text = baseResult as? String
但是,这通常不是 Firebase 结构的显示方式。更像这样
myRootRef
-JYa6s6gij900is
date: "20160413"
weather: "sunny"
-JY9398iias98o4
date: "20160414"
weather: "blowing snow"
在那种情况下。 .Value 将读取整个节点,快照将包含多个节点(如 key:value 对)
处理方法如下:
ref.observeEventType(.Value, withBlock: { snapshot in
for child in snapshot.children {
if let w = child.value.objectForKey("weather") as? String {
print(w)
}
}
})
输出将是
sunny
blowing snow
我正在尝试创建一个简单的 Firebase 应用程序,我之前已经在 Android 上这样做过。我正在学习 swift 并且正在学习 Firebase 提供的基本教程。
我已经通过 CocoaPods 成功添加了 Firebase,但是我无法读取结果:
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.
}
}
错误:
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 数据库:
试试这个:
if let baseResult = snapshot.value as? String {
self.mLabel.text = baseResult
}
通过 .Value 观察将读取节点的全部内容。
所以如果你的结构是这样的
myRootRef
iso-weather-practice: "sunny"
你的活动没问题,可以这样读
self.mLabel.text = baseResult as? String
但是,这通常不是 Firebase 结构的显示方式。更像这样
myRootRef
-JYa6s6gij900is
date: "20160413"
weather: "sunny"
-JY9398iias98o4
date: "20160414"
weather: "blowing snow"
在那种情况下。 .Value 将读取整个节点,快照将包含多个节点(如 key:value 对)
处理方法如下:
ref.observeEventType(.Value, withBlock: { snapshot in
for child in snapshot.children {
if let w = child.value.objectForKey("weather") as? String {
print(w)
}
}
})
输出将是
sunny
blowing snow