Xcode firebase只是简单地写入数据

时间:2018-04-30 21:07:46

标签: swift firebase firebase-realtime-database

我写了这段代码,因为我需要一个app whit 2按钮,当我按下第一个我想在我的FirebaseDB中写video01时,当我按下第二个时我想写video02。 当我编译代码时,我没有看到两个按钮,在我看来错误的SIGABRT

这是我的代码:

import Firebase
import UIKit

class ViewController: UIViewController {

    var refController: DatabaseReference!

    @IBAction func btt_video1(_ sender: Any) {
        refController.child("nowPlaying").setValue("video01")
    }
    @IBAction func btt_video2(_ sender: Any) {
        refController.child("nowPlaying").setValue("video02")
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        //configuring firebase

        FirebaseApp.configure()

        refController = Database().reference().child("parametri");
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

这是我的错误:

2018-04-30 16:31:42.403230-0400 Remote Controller Client[26074:469999] 4.13.0 - [Firebase/Analytics][I-ACS023007] Firebase Analytics v.40200000 started
2018-04-30 16:31:42.407953-0400 Remote Controller Client[26074:469999] 4.13.0 - [Firebase/Analytics][I-ACS023008] To enable debug logging set the following application argument: -FIRAnalyticsDebugEnabled 
2018-04-30 16:31:42.444269-0400 Remote Controller Client[26074:469864] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSDictionaryM setObject:forKeyedSubscript:]: key cannot be nil'
*** First throw call stack:
(
    0   CoreFoundation                      0x000000010f2d01e6 __exceptionPreprocess + 294
    1   libobjc.A.dylib                     0x000000010e3d0031 objc_exception_throw + 48
    2   CoreFoundation                      0x000000010f3100bc _CFThrowFormattedException + 194
    3   CoreFoundation                      0x000000010f3746e2 -[__NSDictionaryM setObject:forKeyedSubscript:] + 1026
    4   Remote Controller Client            0x000000010c857470 +[FRepoManager createRepo:config:database:] + 273
    5   Remote Controller Client            0x000000010c83eb16 -[FIRDatabase ensureRepo] + 151
    6   Remote Controller Client            0x000000010c83e20e -[FIRDatabase reference] + 31
    7   Remote Controller Client            0x000000010c7b6da3 _T024Remote_Controller_Client04ViewB0C11viewDidLoadyyF + 131
    8   Remote Controller Client            0x000000010c7b6f44 _T024Remote_Controller_Client04ViewB0C11viewDidLoadyyFTo + 36
    9   UIKit                               0x000000010f8f1191 -[UIViewController loadViewIfRequired] + 1215
    10  UIKit                               0x000000010f8f15d4 -[UIViewController view] + 27
    11  UIKit                               0x000000010f7bf183 -[UIWindow addRootViewControllerViewIfPossible] + 122
    12  UIKit                               0x000000010f7bf894 -[UIWindow _setHidden:forced:] + 294
    13  UIKit                               0x000000010f7d262c -[UIWindow makeKeyAndVisible] + 42
    14  UIKit                               0x000000010f74643a -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 4739
    15  UIKit                               0x000000010f74b62b -[UIApplication _runWithMainScene:transitionContext:completion:] + 1677
    16  UIKit                               0x000000010fb0de4a __111-[__UICanvasLifecycleMonitor_Compatability _scheduleFirstCommitForScene:transition:firstActivation:completion:]_block_invoke + 866
    17  UIKit                               0x000000010fee0909 +[_UICanvas _enqueuePostSettingUpdateTransactionBlock:] + 153
    18  UIKit                               0x000000010fb0da86 -[__UICanvasLifecycleMonitor_Compatability _scheduleFirstCommitForScene:transition:firstActivation:completion:] + 236
    19  UIKit                               0x000000010fb0e2a7 -[__UICanvasLifecycleMonitor_Compatability activateEventsOnly:withContext:completion:] + 675
    20  UIKit                               0x000000011047f4d4 __82-[_UIApplicationCanvas _transitionLifecycleStateWithTransitionContext:completion:]_block_invoke + 299
    21  UIKit                               0x000000011047f36e -[_UIApplicationCanvas _transitionLifecycleStateWithTransitionContext:completion:] + 433
    22  UIKit                               0x000000011016362d __125-[_UICanvasLifecycleSettingsDiffAction performActionsForCanvas:withUpdatedScene:settingsDiff:fromSettings:transitionContext:]_block_invoke + 221
    23  UIKit                               0x000000011035e387 _performActionsWithDelayForTransitionContext + 100
    24  UIKit                               0x00000001101634f7 -[_UICanvasLifecycleSettingsDiffAction performActionsForCanvas:withUpdatedScene:settingsDiff:fromSettings:transitionContext:] + 223
    25  UIKit                               0x000000010fedffb0 -[_UICanvas scene:didUpdateWithDiff:transitionContext:completion:] + 392
    26  UIKit                               0x000000010f749f0c -[UIApplication workspace:didCreateScene:withTransitionContext:completion:] + 515
    27  UIKit                               0x000000010fd1ca97 -[UIApplicationSceneClientAgent scene:didInitializeWithEvent:completion:] + 361
    28  FrontBoardServices                  0x00000001163932f3 -[FBSSceneImpl _didCreateWithTransitionContext:completion:] + 331
    29  FrontBoardServices                  0x000000011639bcfa __56-[FBSWorkspace client:handleCreateScene:withCompletion:]_block_invoke_2 + 225
    30  libdispatch.dylib                   0x0000000111ee7848 _dispatch_client_callout + 8
    31  libdispatch.dylib                   0x0000000111eece14 _dispatch_block_invoke_direct + 592
    32  FrontBoardServices                  0x00000001163c7470 __FBSSERIALQUEUE_IS_CALLING_OUT_TO_A_BLOCK__ + 24
    33  FrontBoardServices                  0x00000001163c712e -[FBSSerialQueue _performNext] + 439
    34  FrontBoardServices                  0x00000001163c768e -[FBSSerialQueue _performNextFromRunLoopSource] + 45
    35  CoreFoundation                      0x000000010f272bb1 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    36  CoreFoundation                      0x000000010f2574af __CFRunLoopDoSources0 + 271
    37  CoreFoundation                      0x000000010f256a6f __CFRunLoopRun + 1263
    38  CoreFoundation                      0x000000010f25630b CFRunLoopRunSpecific + 635
    39  GraphicsServices                    0x0000000115019a73 GSEventRunModal + 62
    40  UIKit                               0x000000010f74d0b7 UIApplicationMain + 159
    41  Remote Controller Client            0x000000010c7b8497 main + 55
    42  libdyld.dylib                       0x000000010f103955 start + 1
    43  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

有人可以帮助我吗?

2 个答案:

答案 0 :(得分:0)

setValue采用字典

let video1 = ["Video":"Video1"]
refController.child("nowPlaying").setValue(video1)

let video2 = ["Video":"Video2"]
refController.child("nowPlaying").setValue(video2)

答案 1 :(得分:0)

问题是,在某些时候, nil 正被写入某个不允许的地方和/或Firebase未正确配置。

一个重要的故障排除步骤是代码不会编译,这意味着Firebase可能没有正确配置。

要看两件事:

1)您未正确初始化数据库引用。

你拥有的是

  

refController = Database()。reference()。child(" parametri");

它应该是

self.refController = Database.database().reference()

然后,您可以在代码中执行

let paraRef = self.refController.child("parametri")
let nowPlayingRef = paraRef.child("nowPlaying")
nowPlayingRef.setValue("video 1")

将导致像这样的结构

firebase_root
   parametri
      nowPlaying: "video 1"

2)在您的App Delegate中,执行此操作

class AppDelegate: UIResponder, UIApplicationDelegate {

   var window: UIWindow?
   func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
       FirebaseApp.configure()
       return true
   }