无法使用Swift读取Firebase数据

时间:2016-04-14 21:42:21

标签: ios swift firebase unrecognized-selector

我正在尝试创建一个简单的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数据库:

enter image description here

2 个答案:

答案 0 :(得分:2)

试试这个:

if let baseResult = snapshot.value as? String {
    self.mLabel.text = baseResult
}

答案 1 :(得分:2)

观察.Value将读入节点的全部内容。

所以如果你的结构是这样的

path_or_buf

您的活动会好起来,可以像这样阅读

DataFrame.to_csv

然而,通常不会出现Firebase结构的方式。它更像是这个

fxData.to_csv('{0}\{1}{2}{3}'.format(fxRollPath, fxRoll, str(index), '.csv'))

在那种情况下。 .Value将读取整个节点,快照将包含多个节点(作为键:值对)

所以这是如何处理它:

myRootRef
   iso-weather-practice: "sunny"

输出

self.mLabel.text = baseResult as? String