微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Xcode中SwiftUI中的细分11非零退出代码错误

如何解决Xcode中SwiftUI中的细分11非零退出代码错误

错误:分段错误: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)
}

}

此外,当我在内容视图init中声明对象时,我收到一个奇怪的错误,其中指出“ TimeFetcher”无法构造,因为它没有可访问的初始化程序。

init() {
    let vm = viewmodel()
    VModel = vm
    annotationsVM =  AnnotationsVM(VModel: vm)
    locationManager = LocationManager()
    data = DataFetcher()
    timeData = TimeFetcher(data: data)
}

解决方法

@State@ObservedObject应该只在View

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

针对所有@Published粘贴@State,然后在非@ObservedObject类中删除不需要的View

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。