下記のコードでFirebase RealTimeDataBaseにデータを追加したいと考えております。
2にあたる部分の名前をsendtxの値にして、
sendtxとsendtx1をtext1,text2として、
登録したいと考えております。
しかし、下記のコードで実行しましたが、
データが望む通りに新規登録されませんでした。
どのように記述すれば良いかわかりません。
Swift
1 //値を登録する 2 func addData(text : Any){ 3 4 //delegate 5 sendtx1.delegate = self 6 sendtx.delegate = self 7 8 //ここの1行を追加 9 indicator_1.startAnimating() 10 // データベースへの参照 11 let rootRef = Database.database().reference() 12 13 //値を追加する階層を設定 14 let tittle1: String = sendtx.text! 15 16 //この部分でうまく代入されない!! 17 let conditionRef = rootRef.child("TestApp/(tittle1)") 18 //↑うまく代入されない! 19 //想定している代入イメージ (sendtxが1kの場合) "TestApp/1k" 20
ご教授のほど、何卒よろしくお願いいたします。
データの構造図は下記の通りです。
また、全体コードは次の通りです。
Swift
1import UIKit 2 import Firebase 3 import NVActivityIndicatorView 4 5 class ViewController2: UIViewController, UITextFieldDelegate { 6 @IBOutlet weak var sendtx: UITextField! 7 @IBOutlet weak var sendtx1: UITextField! 8 9 10 11 @IBOutlet weak var indicator_1: NVActivityIndicatorView! 12 13 14 @IBAction func theButton(sender: UIButton) { 15 var sendarray:[Dictionary<String,String>] = [] 16 var sendtextArray = ["text1":sendtx.text!,"text2":sendtx1.text!] 17 18 sendtx1.text = "" 19 sendtx.text = "" 20 21 22 addData(text: sendtextArray) 23 } 24 25 // データベースへの参照 26 let rootRef = Database.database().reference() 27 var ref: DatabaseReference! 28 29 30 // データベースへの参照 31 32 var receiveData: String = "" 33 var receiveData2: String = "" 34 35 36 override func viewDidLoad() { 37 super.viewDidLoad() 38 sendtx1.delegate = self 39 sendtx.delegate = self 40 41 42 // Do any additional setup after loading the view. 43 } 44 45 //フィールドをタッチした時に、キーボードを閉じる 46 47 override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 48 sendtx1.resignFirstResponder() 49 sendtx.resignFirstResponder() 50 } 51 52 func textFieldShouldReturn(_ textField: UITextField) -> Bool { 53 textField.resignFirstResponder() 54 return true 55 } 56 57 58 //値を登録する 59 func addData(text : Any){ 60 61 //delegate 62 sendtx1.delegate = self 63 sendtx.delegate = self 64 65 //ここの1行を追加 66 indicator_1.startAnimating() 67 // データベースへの参照 68 let rootRef = Database.database().reference() 69 70 //値を追加する階層を設定 71 let tittle1: String = sendtx.text! 72 73 //この部分でうまく代入されない!! 74 let conditionRef = rootRef.child("TestApp/(tittle1)") 75 //↑うまく代入されない! 76 //想定している代入イメージ (sendtxが1kの場合) "TestApp/1k" 77 78 print(conditionRef) 79 //値を追加 80 conditionRef.setValue(text) 81 82 83 } 84 85 // //参考までに記載 86 // func textFieldShouldReturn(textField: UITextField) -> Bool{ 87 // 88 // let sendData = ["send1": sendtx.text!, "send2": sendtx1.text!] 89 // ref.childByAutoId().setValue(sendData) 90 // 91 // textField.resignFirstResponder() 92 // sendtx.text = "" 93 // sendtx1.text = "" 94 // 95 // return true 96 // } 97 98 // 99 100 override func viewDidAppear(_ animated: Bool) { 101 super.viewDidAppear(animated) 102 103 // 子ノード condition への参照 104 let conditionRef = rootRef.child("condition") 105 // クラウド上で、ノード condition に変更があった場合のコールバック処理 106 conditionRef.observe(.value) { (snap: DataSnapshot) in 107 print("ノードの値が変わりました!: ((snap.value as AnyObject).description)") 108 } 109 } 110 111 /* 112 // MARK: - Navigation 113 114 // In a storyboard-based application, you will often want to do a little preparation before navigation 115 override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 116 // Get the new view controller using segue.destination. 117 // Pass the selected object to the new view controller. 118 } 119 */ 120 121 } 122 123
あなたの回答
tips
プレビュー