質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.47%
Cloud Firestore

Cloud Firestore は、自動スケーリングと高性能を実現し、アプリケーション開発を簡素化するように構築された NoSQLドキュメントデータベースです。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

解決済

1回答

692閲覧

【CloudFireStore】Document IDを指定できない

nishimu

総合スコア26

Cloud Firestore

Cloud Firestore は、自動スケーリングと高性能を実現し、アプリケーション開発を簡素化するように構築された NoSQLドキュメントデータベースです。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

0クリップ

投稿2020/04/11 01:54

編集2020/04/11 02:05

Document IDを指定してデータを追加したい

swift・Cloud Firestoreで学校祭向けに待ち時間を共有するアプリを作っています。
ユーザーは待ち時間を投稿したいクラスをtableViewから選び、出てきたalertのtextFieldに待ち時間を入力します。
このときcollection名はHR,document名はユーザーが選択したクラスで追加したいのですが、document名をユーザーが選択したクラスで追加することができません。
ご教授願います

発生している問題・エラーメッセージ

Cannot assign value of type 'Void' to type 'DocumentReference'

該当のソースコード

swift

1import Foundation 2import Firebase 3 4protocol DocumentSerializable { 5 init?(dictionary:[String:Any]) 6} 7 8struct Contains { 9 10 var waitTime: String 11 var timeStamp: Date 12 var dictionary:[String:Any] { 13 return [ 14 "waitTime": waitTime, 15 "timeStamp": timeStamp 16 ] 17 } 18} 19 20extension Contains: DocumentSerializable { 21 init?(dictionary:[String:Any]){ 22 guard let waitTime = dictionary["waitTime"] as? String, 23 let timeStamp = dictionary["timeStamp"] as? Date 24 25 else {return nil} 26 self.init(waitTime: waitTime, timeStamp: timeStamp) 27 } 28} 29

swift

1import Firebase 2......中略...... 3 4let db = Firestore.firestore() 5......中略...... 6 7 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 8 tableView.deselectRow(at: indexPath, animated: true) 9 let currentHR = displayimageNames[indexPath.row] 10 let alert: UIAlertController = UIAlertController(title: "(currentHR)の待ち時間を更新", message: "現在の待ち時間を入力してください", preferredStyle: UIAlertController.Style.alert) 11 12 let defaultAction: UIAlertAction = UIAlertAction(title: "更新", style: UIAlertAction.Style.default, handler:{ 13 14 (action: UIAlertAction!) -> Void in 15 16 if let waitTime = alert.textFields?.first?.text { 17 18 let newContains = Contains(waitTime: waitTime, timeStamp: Date()) 19 20 // Add a new document with a generated ID 21 var ref: DocumentReference? = nil 22 ref = self.db.collection("HR").document("(currentHR)").setData(newContains.dictionary 23 ){ err in 24 25 if let err = err { 26 print("Error adding document: (err)") 27 } else { 28 print("Document added with ID: (ref!.documentID)") 29 } 30 } 31 32 } 33 34 print("更新") 35 })

試したこと

YouTube SwiftTutorialを参考にさせていただきました
googleDocumentも一部参考にし、試させていただきました
###開発環境
CloudFirestore
xcode11.4

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

Swift

1 2 var ref: DocumentReference? = nil 3 ref = self.db.collection("HR").document("(currentHR)") 4 ref?.updateData(newContains.dictionary 5 ){ err in 6 7 if let err = err { 8 print("Error adding document: (err)") 9 } else { 10 print("Document added with ID: (ref!.documentID)") 11 } 12 } 13 14 } 15 16

こうしたらできました

投稿2020/04/11 06:27

編集2020/04/11 06:27
nishimu

総合スコア26

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.47%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問