【发布时间】:2016-11-25 11:35:26
【问题描述】:
我的一些用户已收到日志,指出这行代码中出现EXC_BREAKPOINT (SIGTRAP) 错误。我一直在努力使其安全,但 EKParticipant 的所有属性都是非可选的,因此与 nil 相比只会给我一个警告,说它永远是正确的。如果这里有什么东西是零,我应该如何处理它?
错误线
let participantEmail : String? = participant.url.absoluteString.lowercased().replacingOccurrences(of: "mailto:", with: "")
Apple 错误说明
跟踪陷阱 [EXC_BREAKPOINT // SIGTRAP]
类似于异常退出,此异常旨在提供 附加的调试器有机会在特定的地方中断进程 指向它的执行。您可以自己触发此异常 使用 __builtin_trap() 函数的代码。如果没有附加调试器, 进程终止并生成崩溃报告。低等级 库(例如 libdispatch)将在遇到 致命错误。有关该错误的其他信息,请参见 崩溃报告的附加诊断信息部分,或 在设备的控制台中。 Swift 代码将终止并出现此异常 如果在运行时遇到意外情况,请键入,例如: 具有 nil 值的非可选类型 强制类型转换失败 查看 Backtraces 以确定遇到意外情况的位置。额外的 信息也可能已记录到设备的控制台。你 应该修改崩溃位置的代码以优雅地处理 运行时失败。例如,使用可选绑定而不是 强制解开一个可选项。”
完整方法
/**
Parses participants for a given event.
Goes through the EKEvents attendees array to build Attendee objects used to model a participant.
- parameter event: The calendar event we'll be finding the participants for.
- returns: An array of Attendee objects with the participants name, email, required/optional status and whether they've accepted their invitation to the event.
*/
private static func parseParticipantsIn(event: EKEvent) -> [Attendee] {
var participants = [Attendee]()
if let attendees = event.attendees, event.attendees?.isEmpty == false {
for participant in attendees {
let participantName : String? = parse(EKParticipantName: participant)
let participantEmail : String? = participant.url.absoluteString.lowercased().replacingOccurrences(of: "mailto:", with: "")
let isRequiredParticipant : Bool = participant.participantRole == EKParticipantRole.required
let hasAccepted : Bool = participant.participantStatus == EKParticipantStatus.accepted
guard (participantName != nil && participantEmail != nil)
else
{
log.error("Participant could not be parsed")
continue
}
let attendee = Attendee(name: participantName!, email: participantEmail!, required: isRequiredParticipant, hasAccepted: hasAccepted)
participants.append(attendee)
}
}
return participants
}
【问题讨论】:
-
这是关于
NRURL的absoluteString返回可选的String吗?let participantEmail : String? = url.absoluteString?.lowercased().replacingOccurrences(of: "mailto:", with: "") -
@SunilChauhan 我已经尝试过了,但我收到一个错误消息,提示
cannot use optional chaining on non optional value of 'String'。 -
那么应该有一些和swift版本有关的东西。你在哪个版本? 2.2 我猜。
-
@SunilChauhan 3.1
-
奇怪。我的是 swift 3.0,除非我把
?和absoluteString放在一起,否则无法编译。