为什么强制展开给我一个EXC_BREAKPOINT (SIGTRAP)错误



我的应用程序在声明for循环for participant in event.attendees!时崩溃了。我对swift比较陌生,并且理解如果我检查与会者数组不是nil,那么我可以自由地强制展开它。我误解了什么?

    private static func parseParticipants(event: EKEvent) -> [Attendee] {
    var participants = [Attendee]()
    if(event.attendees != nil && event.attendees?.count != 0) {
        for participant in event.attendees! {
            let participantName = parseEKParticipantName(participant)
            let isRequiredParticipant = participant.participantRole == EKParticipantRole.Required
            let hasAccepted = participant.participantStatus == EKParticipantStatus.Accepted
            let attendee = Attendee(name: participantName, email: participant.URL.resourceSpecifier!.lowercaseString, required: isRequiredParticipant, hasAccepted: hasAccepted)
            participants.append(attendee)
        }
    }
    return participants
}

事实证明这不是关于强制展开,而是由于EKParticipant.url属性在包含"字符的字符串时返回nil。

let attendee = Attendee(name: participantName, email: participant.URL.resourceSpecifier!.lowercaseString, required: isRequiredParticipant, hasAccepted: hasAccepted)

我们使用它来访问参与者的电子邮件,但是任何对url的读写操作都会导致崩溃,所以我们使用EKParticipant.description属性并使用正则表达式解析电子邮件。

let participantEmail = participant.description.parse(pattern: emailRegex).first ?? ""

使用可选绑定如何?

if let attendees = event.attendees && attendees.count > 0 {
}

相关内容

最新更新