Q&A
前提
Calendarを表示し、選択日と今日の日付の差を取り出して
それを別のビューで四則演算をするシステムを作っております。
まずCalendarTestView内では、
選択日から今日までの日数をdayStringという変数で定義し、
それを@BindingでString型で定義したdayとして返しています。
そのdayをAchievementViewに値渡しし四則演算したいです。
値渡しし、AchivementViewでTextとして表示できたのですが、それをIntに型変換する際にnilエラーが出てしまいます。
実現したいこと
- AchievementView内でdayを四則演算したい
エラーメッセージ
下のソースコードで「ここでエラー」と書いてあるところ、
dayをInt型に変換しているところに以下のエラーメッセージが発生しました。
Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value
dayがなぜnilになっているのか?どのように変えたらnilで無くなるのか?
SwiftUI初心者なので、理由含め教えてくだされば幸いです。
全体のコードはこちらです↓
該当のソースコード
CalendarTestView.swift
1import SwiftUI 2import FSCalendar 3import UIKit 4 5struct CalendarTestView: UIViewRepresentable { 6 @Binding var selectedDate: Date 7 func makeUIView(context: Context) -> UIView { 8 9 typealias UIViewType = FSCalendar 10 11 let fsCalendar = FSCalendar() 12 13 fsCalendar.delegate = context.coordinator 14 fsCalendar.dataSource = context.coordinator 15 16 return fsCalendar 17 } 18 19 func updateUIView(_ uiView: UIView, context: Context) { 20 } 21 22 func makeCoordinator() -> Coordinator{ 23 return Coordinator(self) 24 } 25 26 class Coordinator: NSObject, FSCalendarDelegate, FSCalendarDataSource { 27 var parent:CalendarTestView 28 29 init(_ parent:CalendarTestView){ 30 self.parent = parent 31 } 32 33 func calendar(_ calendar: FSCalendar, didSelect date: Date, at monthPosition: FSCalendarMonthPosition) { 34 // ***** 選択した日付をバインディングのselectedDateへ設定します 35 parent.selectedDate = date 36 } 37 38 } 39} 40 41struct CalendarApealView: View { 42 43 @Environment(\.presentationMode) var presentationMode: Binding<PresentationMode> 44 @State private var date: Date = Date() 45 @Binding var day: String //ここでdayを定義?? 46 var differString: String { 47 let calendar = Calendar.current 48 //today1は今日の日付 49 let today1 = Date() 50 let today2 = calendar.dateComponents([.year, .month, .day], from: today1) 51 let today3 = calendar.date(from: DateComponents(year: today2.year, month: today2.month, day: today2.day))! 52 let date2 = calendar.dateComponents([.year, .month, .day], from: date) 53 let date3 = calendar.date(from: DateComponents(year: date2.year, month: date2.month, day: date2.day))! 54 let result = calendar.dateComponents([.day], from: date3, to: today3) 55// UserDefaults.standard.set(differString, forKey: "differString") 56 return "\(result.day!)" 57 } 58 var body: some View { 59 VStack(){ 60 Text("When did you start this food style?") 61 .font(.title2) 62 CalendarTestView(selectedDate: $date) 63 .frame(height: 400) 64 HStack(){ 65 Text(differString) 66 .font(.title) 67 .padding() 68 .foregroundColor(Color(red: 0.324, green: 0.758, blue: 0.49)) 69 Text("Days") 70 .font(.title2) 71 } 72 Button(action: { 73 day = differString 74 self.presentationMode.wrappedValue.dismiss() 75 }) { 76 Text("confirm") 77 .font(.title2) 78 .padding(16) 79 .background(Color(red: 0.324, green: 0.758, blue: 0.49)) 80 .foregroundColor(Color.white) 81 .cornerRadius(10) 82 } 83 } 84 } 85}
AchievementView.com
1struct AchievementView: View { 2 @State var showsheet: Bool = false 3 @State var showinfo: Bool = false 4 init(){ 5 //ナビゲーションバーの背景色の設定 6 UINavigationBar.appearance().barTintColor = greenColor 7 } 8 9 @State private var day = "" 10 @State private var isShowSubView = false 11 var body: some View { 12 13 NavigationView { 14 HStack { 15 ZStack { 16 VStack{ 17 HStack { 18 ScrollView{ 19 Image("IMG_7771") 20 .resizable() 21 .clipShape(Circle()) 22 .overlay(Circle().stroke(Color.white, lineWidth: 2)) 23 .frame(width: 100, height: 100) 24 .shadow(radius: 5) 25 Text("Name") 26 .font(.title) 27 Text("Name Record ...") 28 .font(.title) 29 HStack{ 30 Text(day) 31 .font(.largeTitle) 32 .foregroundColor(Color(red: 0.324, green: 0.758, blue: 0.49)) 33 34 Text("DAYS") 35 .padding() 36 .font(.title) 37 38 Button(action: { 39 self.isShowSubView = true 40 }, label: { 41 Text("edit") 42 .foregroundColor(Color.gray) 43 }) 44 45 NavigationLink(destination: CalendarApealView(day: $day), isActive: $isShowSubView) { 46 EmptyView() 47 } 48 } 49 HStack{ 50 Text("\((Int(day))!*100)") //ここでエラー 51 .font(.largeTitle) 52 .foregroundColor(Color(red: 0.324, green: 0.758, blue: 0.49)) 53 Text("L") 54 .padding(.leading, 40) 55 .font(.title) 56 } 57 } 58 } 59 } 60 } 61 } 62 .navigationTitle("Achievement") 63 .toolbar{ 64 ToolbarItemGroup(placement: .navigationBarTrailing) { 65 Button { 66 showsheet.toggle() 67 } label: { 68 Label("Setting", systemImage: "gearshape") 69 } 70 .sheet(isPresented: $showsheet, content: { 71 SettingView() 72 }) 73 } 74 } 75 } 76 } 77} 78
補足情報(FW/ツールのバージョンなど)
macOS バージョン12.3.1
2.6 GHz 6コアIntel Core i7
Xcodeバージョン13.3.1
回答1件
あなたの回答
tips
プレビュー
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。
2022/06/05 09:22