0

error: Segmentation fault: 11 (in target 'ProjectName' from project 'ProjectName')

Command CompileSwift failed with a nonzero exit code

Both of these errors are present when the following file is in my project:

import Foundation
import SwiftUI
import Mapbox

class TimeFetcher: ObservableObject {
@Published var startTime: String = ""
@Published var endTime: String = ""
@Published var eventdate: String = ""
@Published var annotation: MGLAnnotation?
@Published var eventdate: String = ""
@Published var date: Date? = Date()
@Published var startTimeDateObject: Date? = Date()
@Published var endTimeDateObject: Date? = Date()
var data: DataFetcher

var inputDateFormatter: DateFormatter = {
    let formatter = DateFormatter()
    formatter.locale = Locale(identifier: "en_US_POSIX")
    formatter.timeZone = TimeZone(abbreviation: "UTC")
    formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ" // <- do not escape `Z` (time zone)
    return formatter
}()

var outputDateFormatter: DateFormatter = {
    let formatter = DateFormatter()
    formatter.timeZone = TimeZone.current
    formatter.setLocalizedDateFormatFromTemplate("EEEE MMMM yyyy d") //hh mm")
    return formatter
}()

var outputTimeFormatter: DateFormatter = {
    let formatter = DateFormatter()
    formatter.timeZone = TimeZone.current
    formatter.setLocalizedDateFormatFromTemplate("hh mm")
    return formatter
}()

var formattedDate: String {
    date != nil ? self.outputDateFormatter.string(from: date!) : "Date is nil"
}

var formattedStartTime: String {
    startTimeDateObject != nil ? self.outputTimeFormatter.string(from: startTimeDateObject!) : "Date is nil"
}

var formattedEndTime: String {
    endTimeDateObject != nil ? self.outputTimeFormatter.string(from: endTimeDateObject!) : "Time is nil"
}


func setStartAndEndTimes() {
    for event in self.data.events {
        print("the selectedannotation title is \(annotation.title) and the event address is \(annotation.address)")
        if self.annotation.title == event.address {
            print("the start time is initially \(self.startTime)")
            self.startTime = event.start_time
            print("the start time at the end is \(self.startTime)")
            self.endTime = event.end_time
        }
    }
}

func parseDate() {
    let dateStr = self.eventdate
    if let date = self.inputDateFormatter.date(from: dateStr) {
        self.date = date
    } else {
        print("invalid date")
    }
    // alternatively if you don't care about errors:
    // date = Self.dateFormatter.date(from: dateStr)
}
func parseStartTime() {
    let dateStr = self.startTime
    if let date = self.inputDateFormatter.date(from: dateStr) {
        self.startTimeDateObject = date
    } else {
        print("invalid date")
    }
    // alternatively if you don't care about errors:
    // date = Self.dateFormatter.date(from: dateStr)
}
func parseEndTime() {
    let dateStr = self.endTime
    if let date = self.inputDateFormatter.date(from: dateStr) {
        self.endTimeDateObject = date
    } else {
        print("invalid date")
    }
    // alternatively if you don't care about errors:
    // date = Self.dateFormatter.date(from: dateStr)
}

}

Also when I declare the object in my content view init, I get a weird error where it says 'TimeFetcher' cannot be constructed because it has no accessible initializers'

init() {
    let vm = ViewModel()
    VModel = vm
    annotationsVM =  AnnotationsVM(VModel: vm)
    locationManager = LocationManager()
    data = DataFetcher()
    timeData = TimeFetcher(data: data)
}
nickcoding
  • 305
  • 8
  • 35

1 Answers1

3

@State and @ObservedObject should only be in a View

https://developer.apple.com/documentation/swiftui/state https://developer.apple.com/documentation/swiftui/observedobject

Stick with @Published for all the @State and remove the @ObservedObject you don't need it in a non-View class.

lorem ipsum
  • 21,175
  • 5
  • 24
  • 48
  • I changed the State variables to published variables but the ObservedObject var data: DataFetcher line has to stay there because it is used in one of the functions. The Segmentation 11 error and the non zero exit code errors still persist though :( – nickcoding Aug 29 '20 at 21:51
  • 1
    @nickcoding Remove the word `@ObservedObject`, not the whole line. – pawello2222 Aug 29 '20 at 21:58
  • @pawello2222 I did that, the errors are still there though (the ContentView initialization one was resolved though). Is this something wrong with my code or is this an Apple bug? And if so how would you recommend resolving it? – nickcoding Aug 29 '20 at 22:02
  • Also inside the file itself, it says "An internal error occurred. Source editor functionality is limited. Attempting to restore..." – nickcoding Aug 29 '20 at 22:04
  • 1
    @nickcoding This usually happens when you have two @Published properties with the same name (here `eventdate`). – pawello2222 Aug 29 '20 at 22:05