2017-09-05 6 views
0

私は自分のデータベースに基づいてデータベースを設定しています。私はすべての初期設定をした:私はFirebaseデータベースの日付としてキーを挿入しようとするとクラッシュします

AppDelegate下のコードを配置します:

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

     GIDSignIn.sharedInstance().clientID = FirebaseApp.app()?.options.clientID 
     GIDSignIn.sharedInstance().delegate = self 
     FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions) 

     if (Auth.auth().currentUser) != nil { 
      presentHome() 
     } else { 
      presentSignIn() 
     } 

     return true 
    } 

問題は、私は、データベースの参照とインサートたものを作成するとき、私はこのエラーが出るということです。

terminating with uncaught exception of type NSException

のViewController:

var ref: DatabaseReference! 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     ref = Database.database().reference() 
    } 

@IBAction func saveTask(_ sender: Any) { 
     let task:[String:Any] = [ 
      "title":self.titleTxtField.text!, 
      "type":type!, 
      "observations":"Teste de tarefa", 
      "finished":false, 
      "images":[ 
       "image1":"https://static.pexels.com/photos/248797/pexels-photo-248797.jpeg", 
       "image2":"http://www.planwallpaper.com/images#static/images/beautiful-sunset-images-196063.jpg" 
       ] 
     ] 

     let date = Date().timeIntervalSince1970 
     ref.child("\(self.user!.uid)/tasks/\(date)").setValue(["teste":"teste"]) 
    } 

答えて

1

私はあなたの問題は、このライン上にあると思う:

let date = Date().timeIntervalSince1970 

この印刷そうFirebase

上の子キーとして入力することが許可されていないいくつかの文字、あなたがこのような構造たい場合:

root 
--- year 
------ month 
--------- day 

ことは、これを行うにしてみてください。

  1. 日付からDateComponentsを抽出する

  2. は結果

と新しい文字列を作成し、このコードを子として新しい文字列を追加します。

let components = Calendar.current.dateComponents([.day, .month, .year], from: yourDate) 
let ref = Database.database().reference() 
ref.child("root/\(components.year!)/\(components.month!)/\(components.day!)") 
+0

これは私の問題ではありません。私は日付を削除し、単純な文字列を渡す場合私は同じエラーがある –

+0

、エラーコードのいくつかの画面を提供してくださいと私が調査します –

+0

あなたは正しいです。しかし、私も設定エラーがありました。 2つを打つと、私は自分のデータを記録することができました! –

関連する問題