0

I want to save some data from a SwiftUI view to a Model so that I can use these data into another SwiftUI view. However, I came up with some error when I try to call the Model class and pass all the data as parameters. The error says:

Cannot use instance member 'expectedEndDate' within property initializer; property initializers run before 'self' is available"

Here is my SearchBikeDataModel() code:

import Foundation

class SearchBikeDataModel: ObservableObject {
    @Published var startDate: Date = Date()
    @Published var startTime: Date = Date()
    @Published var endDate: Date = Date()
    @Published var endTime: Date = Date()

    @Published var bikeType: String = ""
    @Published var collectionPoint: String = ""
    @Published var returnPoint: String = ""

    init(selectedStartDate: Date, selectedStartTime: Date, selectedEndDate: Date, selectedEndTime: Date, bikeType: String, collectionPoint: String, returnPoint: String) {
        self.startDate = selectedStartDate
        self.startTime = selectedStartTime
        self.endDate = selectedEndDate
        self.endTime = selectedEndTime

        self.bikeType = bikeType
        self.collectionPoint = collectionPoint
        self.returnPoint = returnPoint
    }
}

And here is the code where I try to pass data as parameters:

import SwiftUI

struct BikeSearchFormView: View {
    @Binding var isDateTimeShown: Bool
    @Binding var isEndDateTimePickerShown: Bool

    @State var expectedStartDate = Date()
    @State var expectedStartTime = Date()
    @State var expectedEndDate = Date()
    @State var expectedEndTime = Date()

    @State var isBikeTypePickerExpand: Bool = false
    @State var isDropOffPointPickerExpand: Bool = false
    @State var isPickUpPointPickerExpand: Bool = false

    @State var selectedBikeType: String = "BIKE TYPE"
    @State var selectedDropOffPoint: String = "DROP OFF POINT"
    @State var selectedPickUpPoint: String = "PICKUP POINT"

    @State var findBikeError: String = ""
    @State var isActive: Bool = false

    @ObservedObject var bikeTypeViewModel = VehicleTypeViewModel()
    @ObservedObject var findBikeViewModel = FindBikeViewModel()

    @ObservedObject var dataModel = SearchBikeDataModel(selectedStartDate: expectedStartDate, selectedStartTime: expectedStartTime, selectedEndDate: expectedEndDate, selectedEndTime: expectedEndTime, bikeType: selectedBikeType, collectionPoint: selectedPickUpPoint, returnPoint: selectedDropOffPoint)

var body: some View {
    Text("Hello, World")
}
}

I have omitted codes of my UI as the question is just about am I following the right way to pass the data into parameters.

James Z
  • 12,209
  • 10
  • 24
  • 44
Shawkath Srijon
  • 739
  • 1
  • 8
  • 17
  • 1
    I would settle a rule: if there are more than three (or even two) states in a view - it must be separated apart. – Asperi May 03 '20 at 10:07
  • Well. Yes, the code got too long. Therefore, I was feeling the same, and will concentrate on this modification after implementation of the functionality that I am try for now. Thanks for suggestion. – Shawkath Srijon May 03 '20 at 10:16

0 Answers0