2017-02-05 15 views
0

私は公式Firebase YouTubeアカウント(https://www.youtube.com/watch?v=joVi3thZOqc)のチュートリアル以下だったが、私はこの単純なアプリを取得するためにマニュアルを読み、変更のカップルを適用しなければならなかったので、それは古いseemes正しく機能します。私はAppDelegate.swiftファイルにlldbエラーが表示されます。スウィフトはシンプルなアプリに - lldbエラー

import UIKit 
import Firebase 
import FirebaseDatabase 

@UIApplicationMain 
class AppDelegate: UIResponder, UIApplicationDelegate { //error here 

    var window: UIWindow? 

    override init(){ 
     FIRApp.configure() 
    } 

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 
     // Override point for customization after application launch. 
     return true 
    } 

ViewController.swiftファイル:

import UIKit 
import Firebase 
import FirebaseDatabase 

class ViewController: UIViewController { 

    //FBDataBase variables 
    var ref: FIRDatabaseReference = FIRDatabase.database().reference() //root directory 

    //interface variables 
    @IBOutlet weak var label: UILabel! 

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

    override func viewDidAppear(_ animated: Bool) { 
     super.viewDidAppear(animated) 

     ref.observe(.value) { (snapshot) in 
      self.label.text = snapshot.value as? String 
     } 
    } 

    //interface functions 
    @IBAction func sunnyPressed(_ sender: Any) { 
     ref.child("condition").child("addedByUser").setValue("sunny") 
    } 

    @IBAction func foggyPressed(_ sender: Any) { 
     ref.child("condition").child("addedByUser").setValue("foggy") 

    } 
} 

コンソールログ:

2017-02-05 13:12:40.738 fireDataBaseTest[4745:140438] Firebase automatic screen reporting is enabled. Call +[FIRAnalytics setScreenName:setScreenClass:] to set the screen name or override the default screen class name. To disable automatic screen reporting, set the flag FirebaseAutomaticScreenReportingEnabled to NO in the Info.plist 
2017-02-05 13:12:40.848 fireDataBaseTest[4745] <Debug> [Firebase/Core][I-COR000001] Configuring the default app. 
2017-02-05 13:12:40.864 fireDataBaseTest[4745:] <FIRAnalytics/INFO> Firebase Analytics v.3600000 started 
2017-02-05 13:12:40.865 fireDataBaseTest[4745:] <FIRAnalytics/INFO> To enable debug logging set the following application argument: -FIRAnalyticsDebugEnabled (see http://goo.gl/...) 
2017-02-05 13:12:40.870: <FIRInstanceID/WARNING> FIRInstanceID AppDelegate proxy enabled, will swizzle app delegate remote notification handlers. To disable add "FirebaseAppDelegateProxyEnabled" to your Info.plist and set it to NO 
2017-02-05 13:12:40.913 fireDataBaseTest[4745:] <FIRAnalytics/INFO> Successfully created Firebase Analytics App Delegate Proxy automatically. To disable the proxy, set the flag FirebaseAppDelegateProxyEnabled to NO in the Info.plist 
2017-02-05 13:12:41.027 fireDataBaseTest[4745:140438] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<fireDataBaseTest.ViewController 0x7fd09e60e1f0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key foggyPressed.' 
*** First throw call stack: 
(
    0 CoreFoundation      0x00000001110cdd4b __exceptionPreprocess + 171 
    1 libobjc.A.dylib      0x0000000110b2f21e objc_exception_throw + 48 
    2 CoreFoundation      0x00000001110cdc99 -[NSException raise] + 9 
    3 Foundation       0x000000011063d9df -[NSObject(NSKeyValueCoding) setValue:forKey:] + 291 
    4 UIKit        0x0000000111d9f293 -[UIViewController setValue:forKey:] + 88 
    5 UIKit        0x000000011201379e -[UIRuntimeOutletConnection connect] + 109 
    6 CoreFoundation      0x00000001110729e0 -[NSArray makeObjectsPerformSelector:] + 256 
    7 UIKit        0x0000000112012122 -[UINib instantiateWithOwner:options:] + 1867 
    8 UIKit        0x0000000111da59c5 -[UIViewController _loadViewFromNibNamed:bundle:] + 386 
    9 UIKit        0x0000000111da62e7 -[UIViewController loadView] + 177 
    10 UIKit        0x0000000111da661c -[UIViewController loadViewIfRequired] + 201 
    11 UIKit        0x0000000111da6e70 -[UIViewController view] + 27 
    12 UIKit        0x0000000111c704b5 -[UIWindow addRootViewControllerViewIfPossible] + 71 
    13 UIKit        0x0000000111c70c06 -[UIWindow _setHidden:forced:] + 293 
    14 UIKit        0x0000000111c84519 -[UIWindow makeKeyAndVisible] + 42 
    15 UIKit        0x0000000111bfcf8d -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 4818 
    16 UIKit        0x0000000111c030ed -[UIApplication _runWithMainScene:transitionContext:completion:] + 1731 
    17 UIKit        0x0000000111c0026d -[UIApplication workspaceDidEndTransaction:] + 188 
    18 FrontBoardServices     0x00000001164a56cb __FBSSERIALQUEUE_IS_CALLING_OUT_TO_A_BLOCK__ + 24 
    19 FrontBoardServices     0x00000001164a5544 -[FBSSerialQueue _performNext] + 189 
    20 FrontBoardServices     0x00000001164a58cd -[FBSSerialQueue _performNextFromRunLoopSource] + 45 
    21 CoreFoundation      0x0000000111072761 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17 
    22 CoreFoundation      0x000000011105798c __CFRunLoopDoSources0 + 556 
    23 CoreFoundation      0x0000000111056e76 __CFRunLoopRun + 918 
    24 CoreFoundation      0x0000000111056884 CFRunLoopRunSpecific + 420 
    25 UIKit        0x0000000111bfeaea -[UIApplication _run] + 434 
    26 UIKit        0x0000000111c04c68 UIApplicationMain + 159 
    27 fireDataBaseTest     0x000000010f40409f main + 111 
    28 libdyld.dylib      0x0000000113d8a68d start + 1 
) 
libc++abi.dylib: terminating with uncaught exception of type NSException 

私はスウィフトとFirebaseに新たなんだので、私は」をいくつかの助けを得ることを楽しみにしています。修正はおそらく非常に簡単で、スキルの欠如のために何かが欠けているだけです。

+1

おそらくあなたのストーリーボードには、クラスを正しく設定していない可能性があります。 – ctabuyo

+0

ViewController.swiftにViewControllerクラスを設定していないということですか?私はそれを疑う、それはデフォルトで設定され、アプリケーションは、Firebaseを含むコード行のカップルのカップルを適用する前に実行していた – codddeer123

+0

私は、エラーメッセージのテストビューコントローラがデフォルトで追加されたとは思わない、霧がかかった "キー... – Mundi

答えて

2

あなたはすでにエラーを考え出したが、指導のためのいくつかのことを追加します。

アプリデリゲートは、この(スウィフト3、Firebase 3)のようになります。他にもオプションがあります。これは単なる出発点に過ぎない。

class AppDelegate: UIResponder, UIApplicationDelegate { 

    var window: UIWindow? 

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

のViewControllerは、お使いのインタフェース機能

import UIKit 
import Firebase 

    class ViewController: UIViewController { 

     var ref: FIRDatabaseReference! 

し、あなたのviewDidLoadメソッド今

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

    ref = FIRDatabase.database().reference() 
} 

次のようになります。

@IBAction func sunnyPressed(_ sender: Any) { 
    ref.child("condition").child("addedByUser").setValue("sunny") 
} 

あなたはそれが次のノードを書きますよう

rootRef 
    condition 
    addedByUser: "sunny" 

あなたは-Yu99sj9jkoeがchildByAutoIdで作成され

rootRef 
    conditions 
    -Yu99sj9jkoe 
     addedByUser: "users uid" 
     condition: "snowing" 
     timestamp: "20170205075600" 
     location: "Mountains" 

ような何かをしたいことがありFirebaseすることをやりたいするつもりはないことを確認 - とrestは、その子ノードに書き込まれた辞書です。

ああ - あなたはFirebaseコンソールから生成されるだけでなく、あなたのGoogleService-Info.plistファイルをインポートしていることを確認してください。

編集:コメントへのフォローアップ。どのように条件ノード内のデータを取得しますか。

そこにはいくつかの方法がありますが、しばらくの間、私たちは特定のノードの子データを取得したいと言います。ここでは、それらのすべてをで読みたい場合は、あなたが

allConditionsRef.observeEvent(.value, with: { snapshot in 

にそれを変更するコード

let conditionsRef = rootRef.child("conditions") 
let thisConditionRef = conditionsRef.child("-Yu99sj9jkoe") 
thisConditionRef.observeSingleEvent(of: .value, with: { snapshot in 
    let conditionDict = snapshot.value as! [String:AnyObject] 
    let uid = conditionDict["addedByUser"] as! String 
    let location = conditionDict["location"] as! String 
    print("uid \(uid) is located in the \(location)" 
}) 

だしかし、あなたは、スナップショットを反復し、それぞれの子に対処する必要があります。それを行う方法については、多くの記事があります。

+0

答えをありがとう。私は 'ref = FIRDatabase.database()。reference()'を 'viewDidAppear'の中に入れてはいけません。アプリの読み込み時に作成する方が理にかなっています。 2番目の質問:それはより自明の説明を作成する方が良いですか?ですから、ルート/条件/条件を取得するには、 "ref.child("条件 ")。子("条件 ").setValue(" sunny ")'を作成する必要があります。 – codddeer123

+0

別の質問 - 下位レベルでデータを取得する方法?文字列 印刷( "\(snapshot.value) self.label.text = snapshot.valueに{(スナップショット):?私は、ルート/条件/タイプと' ref.observe(とFIRDataEventType.valueを、持っていれば") })'下位レベル( "条件"と "タイプ")からデータを取得することは可能ですか、 'ref.child("条件 ")を作成する必要があります。 – codddeer123

+1

ViewDidLoadはViewDidAppearの前にありますので、ライフサイクルのセクションで[NSViewController](https://developer.apple.com/reference/appkit/nsviewcontroller)を参照してください。 。これは、ノード名の関連付けを解除するのがベストプラクティスです(キーは。この場合-Yu99sj9jkoeで)彼らはそうchildByAutoIdが条件、タイムスタンプと場所を追加し、それらを条件ノードの下に個別に保存されている各ユーザーを可能に活用含まれるデータから。 – Jay

関連する問題