错误:分段错误:11(在项目“ProjectName”的目标“ProjectName”中)
命令 CompileSwift 因非零退出代码而失败
当我的项目中有以下文件时,这两个错误都存在:
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)
}
}
此外,当我在我的内容视图初始化中声明对象时,我收到一个奇怪的错误,它说“无法构造 TimeFetcher”,因为它没有可访问的初始化程序”
init() {
let vm = ViewModel()
VModel = vm
annotationsVM = AnnotationsVM(VModel: vm)
locationManager = LocationManager()
data = DataFetcher()
timeData = TimeFetcher(data: data)
}