下の画像のように日付と時間を含む予定が格納されているデータベースから、現在の日付、時刻から直近の予定を判別し、画面上に表示したいのですが、うまくいきません。いろいろ試してみて下記のコードまで行ったのですが、getFromFirestore関数のfor文以降が動いていないようで、コンソールに「OK」が出力されません
解決策を教えていただけないでしょうか。
Swift
1import UIKit 2import Firebase 3 4class HomeViewController: UIViewController { 5 @IBOutlet weak var nextSchedule: UILabel! 6 @IBOutlet weak var kyou: UILabel! 7 8 9 var damy: Int = 0 10 var flag: Int = 0 11 12 var db: Firestore! 13 14 // 現在の日付と時刻 15 static var today = "" 16 static var current = "aaa" 17 18 static var scheduleTime = "" 19 static var scheduleEvent = "" 20 static var userName = "Taro" 21 22 // 現在時刻とイベント時刻の差の絶対値 23 var abs_time = 2400 24 25 // イベントの時間を一時的に保存 26 var tmp_time = "" 27 override func viewDidLoad() { 28 super.viewDidLoad() 29 30 let now = NSDate() 31 let dateFormatter = DateFormatter() 32 let timeFormatter = DateFormatter() 33 dateFormatter.dateFormat = "yyyy/MM/dd" 34 timeFormatter.dateFormat = "HH:mm" 35 nextSchedule.numberOfLines = 0 36 let settings = FirestoreSettings() 37 Firestore.firestore().settings = settings 38 db = Firestore.firestore() 39 getFromFirestore() 40 } 41 42 override func viewWillAppear(_ animated: Bool) { 43 navigationController?.setNavigationBarHidden(true, animated: true 44 ) 45 let now = NSDate() 46 let dateFormatter = DateFormatter() 47 dateFormatter.dateFormat = "yyyy/MM/dd" 48 HomeViewController.today = dateFormatter.string(from: now as Date) 49 kyou.text=HomeViewController.today 50 print(HomeViewController.today 51 52 } 53 54 func nearlyEvent(_ time: String, _ category: String) { 55 let event_time = spiltTime(time) 56 let currunt_time = spiltTime(HomeViewController.current)(HomeViewController.current)") 57 if ((abs(event_time - currunt_time) <= abs_time) && (event_time - currunt_time >= 0)) { 58 abs_time = abs(event_time - currunt_time) 59 HomeViewController.scheduleTime = time 60 HomeViewController.scheduleEvent = category 61 self.nextSchedule.text = HomeViewController.scheduleTime + HomeViewController.scheduleEvent 62 } 63 if (HomeViewController.scheduleTime == HomeViewController.current) { 64 abs_time = 2400 65 self.nextSchedule.text = "" 66 } 67 } 68 69 func spiltTime(_ time: String) -> Int { 70 let split_time = time.components(separatedBy: ":") 71 return Int(split_time[0] + split_time[1])! 72 } 73 74 func changeFlag(_ category: String) -> Int { 75 switch category { 76 case "食事": 77 return 1 78 case "勉強": 79 return 2 80 case "ハミガキ": 81 return 3 82 case "就寝": 83 return 4 84 case "起床": 85 return 5 86 case "薬": 87 return 6 88 case "その他": 89 return 7 90 default: 91 return 0 92 } 93 } 94 95 @objc func getFromFirestore() { 96 let _: Void = db.collection("Taro") 97 .whereField("date", isEqualTo: HomeViewController.today) 98 .getDocuments() { (querySnapshot, err) in 99 if let err = err { 100 print("Error getting documents: (err)") 101 } else { 102 for document in querySnapshot!.documents { 103 print("(document.documentID) => (document.data())") 104 let sample: Dictionary = document.data() 105 let time = sample["time"] as! String 106 let category = sample["category"] as! String 107 self.nearlyEvent(time, category) 108 self.flag = self.changeFlag(HomeViewController.scheduleEvent) 109 print("OK") 110 } 111 } 112 } 113 } 114} 115
回答1件
あなたの回答
tips
プレビュー