前提・実現したいこと
初心者から始める日めくりカレンダーアプリの作り方という動画を見ながら、アプリを作成しています。
カレンダーから今日の日付を取ってきて年月日を表示
曜日によって異なる画像を表示し、土曜日と日曜日は文字色が黒から変わる
指を上下に向かってスライドするとカレンダーがめくれる
という主な仕様をもつ日めくりカレンダーです。
この動画で説明されているコードでは、曜日に合わせて画像が表示される仕様でしたが、
1~31の日付に合わせて画像を表示させようとしています。
発生している問題・エラーメッセージ
ビルドはできるのですが、シュミレーターでは白い画面のままでアプリは立ち上がりません。
プログラムに表示されるエラーとアラートは以下です。
①エラー
swift
1images = [UIImage(named:"1.png")!,UIImage(named:"2.png")!,UIImage(named:"3.png")!, 2UIImage(named:"4.png")!, UIImage(named:"5.png")!, UIImage(named:"6.png")!, 3UIImage(named:"7.png")!,UIImage(named:"8.png")!, UIImage(named:"9.png")!, 4UIImage(named:"10.png")!, UIImage(named:"11.png,")!, UIImage(named:"12.png,")!, 5UIImage(named:"13.png")!, UIImage(named:"14.png")!, UIImage(named:"15.png")!, 6UIImage(named:"16.png")!, UIImage(named:"17.png")!, UIImage(named:"18.png")!, 7UIImage(named:"19.png")!, UIImage(named:"20.png")!, UIImage(named:"21.png")!, 8UIImage(named:"22.png")!,UIImage(named:"23.png")!, UIImage(named:"24.png")!, 9UIImage(named:"25.png")!, UIImage(named:"26.png")!, UIImage(named:"27.png")!, 10UIImage(named:"28.png")!, UIImage(named:"29.png")!, UIImage(named:"30.png")!, 11UIImage(named:"31.png")!]
の箇所に対して
Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value
②アラート
swift
1 if let weekDay = weekComponent.weekday{ 2 //if文内なら、weekDayはInt型 3 //日付-1のインデックスの画像を表示する 4 imageView.image = images[dayNum-1] as? UIImage //アラート1 5 weekdayLabel.text = weekDays[weekDay-1] as? String //アラート2 6 }else{ 7 //weekComponent.weekday == nil の場合の処理 8 } 9
の箇所に対して
Conditional cast from 'UIImage' to 'UIImage' always succeeds Conditional cast from 'String' to 'String' always succeeds
該当のソースコード
swift
1import UIKit 2 3class ViewController: UIViewController { 4 5 @IBOutlet var yearLabel:UILabel! 6 @IBOutlet var monthLabel:UILabel! 7 @IBOutlet var dayLabel:UILabel! 8 @IBOutlet var imageView:UIImageView! 9 @IBOutlet var weekdayLabel:UILabel! 10 11 var images: [UIImage] = [] 12 var weekDays: [String] = [] 13 14 var dayCount = 0; 15 16 override func viewDidLoad() { 17 super.viewDidLoad() 18 // Do any additional setup after loading the view, typically from a nib. 19 20 /* 21 images = [UIImage(named:"sunday.png")!, UIImage(named:"monday.png")!, UIImage(named:"tuesday.png")!, UIImage(named:"wednesday.png")!, UIImage(named:"thursday.png")!, UIImage(named:"friday.png")!, UIImage(named:"saturday.png")!] 22 */ 23 24 images = [UIImage(named:"1.png")!,UIImage(named:"2.png")!,UIImage(named:"3.png")!, UIImage(named:"4.png")!, UIImage(named:"5.png")!, UIImage(named:"6.png")!, UIImage(named:"7.png")!,UIImage(named:"8.png")!, UIImage(named:"9.png")!, UIImage(named:"10.png")!, UIImage(named:"11.png,")!, UIImage(named:"12.png,")!, UIImage(named:"13.png")!, UIImage(named:"14.png")!, UIImage(named:"15.png")!, UIImage(named:"16.png")!, UIImage(named:"17.png")!, UIImage(named:"18.png")!, UIImage(named:"19.png")!, UIImage(named:"20.png")!, UIImage(named:"21.png")!, UIImage(named:"22.png")!,UIImage(named:"23.png")!, UIImage(named:"24.png")!, UIImage(named:"25.png")!, UIImage(named:"26.png")!, UIImage(named:"27.png")!, UIImage(named:"28.png")!, UIImage(named:"29.png")!, UIImage(named:"30.png")!, UIImage(named:"31.png")!] 25 26 27 weekDays = ["日曜日", "月曜日", "火曜日", "水曜日", "木曜日", "金曜日", "土曜日"] 28 29 showDate() 30 31 //actions 32 let leftSwipe = UISwipeGestureRecognizer(target: self, action: Selector(("prevDay"))) 33 leftSwipe.direction = .left 34 self.view.addGestureRecognizer(leftSwipe) 35 36 let rightSwipe = UISwipeGestureRecognizer(target: self, action: Selector(("nextDay"))) 37 rightSwipe.direction = .right 38 self.view.addGestureRecognizer(rightSwipe) 39 40 let upSwipe = UISwipeGestureRecognizer(target: self, action: Selector(("nextvDay"))) 41 leftSwipe.direction = .up 42 self.view.addGestureRecognizer(upSwipe) 43 44 let downSwipe = UISwipeGestureRecognizer(target: self, action: Selector(("prevDay"))) 45 rightSwipe.direction = .down 46 self.view.addGestureRecognizer(downSwipe) 47 48 49 } 50 51 func showDate(){ 52 //count seconds to date 53 let seconds = TimeInterval(dayCount*24*60*60) 54 //date and time 55 let now = NSDate(timeIntervalSinceNow: seconds) 56 57 let dateFormatter = DateFormatter() 58 //place 59 dateFormatter.locale = NSLocale(localeIdentifier:"ja_JP") as Locale? 60 61 dateFormatter.dateFormat = "yyyy" 62 let year = dateFormatter.string(from: now as Date) 63 yearLabel.text = year 64 65 dateFormatter.dateFormat = "M" 66 let month = dateFormatter.string(from: now as Date) 67 monthLabel.text = month 68 69 dateFormatter.dateFormat = "d" 70 let day = dateFormatter.string(from: now as Date) 71 dayLabel.text = day 72 73 //aquire current calendar 74 let calendar = NSCalendar(calendarIdentifier: NSCalendar.Identifier.gregorian)! //現在のカレンダー 75 let weekComponent = calendar.components(.weekday, from: now as Date) 76 let weekDay = weekComponent.weekday // 曜日 77 78 let dayNum = Int(day)! 79 80 if let weekDay = weekComponent.weekday{ 81 //if文内なら、weekDayはInt型 82 //日付-1のインデックスの画像を表示する 83 imageView.image = images[dayNum-1] as? UIImage 84 weekdayLabel.text = weekDays[weekDay-1] as? String 85 }else{ 86 //weekComponent.weekday == nil の場合の処理 87 } 88 89 90 /* 91 imageView.image = images[Int(day)!-1] as? UIImage 92 weekdayLabel.text = weekDays[weekDay!-1] as? String 93 */ 94 95 switch(weekDay){ 96 //日曜日は文字色赤 97 case 1: 98 dayLabel.textColor = UIColor.red 99 weekdayLabel.textColor = UIColor.red 100 //土曜日は文字色青 101 case 7: 102 dayLabel.textColor = UIColor.blue 103 weekdayLabel.textColor = UIColor.blue 104 //他は黒 105 default: 106 dayLabel.textColor = UIColor.black 107 weekdayLabel.textColor = UIColor.black 108 109 } 110 } 111 112 //back to the previous date 113 func prevDay(){ 114 dayCount -= 1 115 UIView.beginAnimations("TransitionAnimation", context: nil) 116 UIView.setAnimationTransition(UIViewAnimationTransition.curlDown, for: self.view, cache: true) 117 UIView.setAnimationDuration(1) 118 showDate() 119 UIView.commitAnimations() //how to change UI 120 } 121 122 func nextDay(){ 123 dayCount += 1 124 UIView.beginAnimations("TransitionAnimation", context: nil) 125 UIView.setAnimationTransition(UIViewAnimationTransition.curlUp, for: self.view, cache: true) 126 UIView.setAnimationDuration(1) 127 showDate() 128 UIView.commitAnimations() 129 } 130 131 override func didReceiveMemoryWarning() { 132 super.didReceiveMemoryWarning() 133 // Dispose of any resources that can be recreated. 134 } 135 136 137} 138 139
試したこと
エラー文に対しては、画像がアップロードされていることは確認しましたが、
他に確認すべき点や改善方法の見当がつかず困っています。
補足情報(FW/ツールのバージョンなど)
Mac OS Catalina 10.15.1
Xcode Version 11.2.1
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。