作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
如果用户输入的年份低于 1948 年或例如高于 2086 年,我的 Xcode 应用程序会崩溃,并且想知道以前是否有人遇到过这个问题以及如何解决?
用户选择他们的出生日期,该应用程序会显示数据,例如今天的日期和他们的出生日期之间的秒数。
我是 Xcode (Xcode 8) 的新手,所以还在学习中。提前致谢
@IBAction func clickScreenBtn(_ sender: Any) {
let birthDay = UserDefaults.standard.object(forKey: "birthday") as! Date
var component = NSCalendar.current.dateComponents([.day], from: birthDay, to: Date())
type = type + 1
switch type {
case 0:
unitLabel.text = "Days"
timeLabel.text = ""
timeLabel.text = timeLabel.text?.appendingFormat("%d", component.day!)
break
case 4:
type = 0
unitLabel.text = "Days"
timeLabel.text = ""
timeLabel.text = timeLabel.text?.appendingFormat("%d", component.day!)
break
case 1:
component = NSCalendar.current.dateComponents([.hour], from: birthDay, to: Date())
unitLabel.text = "Hours"
timeLabel.text = ""
timeLabel.text = timeLabel.text?.appendingFormat("%d", component.hour!)
break
case 2:
component = NSCalendar.current.dateComponents([.minute], from: birthDay, to: Date())
unitLabel.text = "Minutes"
timeLabel.text = ""
timeLabel.text = timeLabel.text?.appendingFormat("%d", component.minute!)
break
case 3:
component = NSCalendar.current.dateComponents([.second], from: birthDay, to: Date())
unitLabel.text = "Seconds"
timeLabel.text = ""
timeLabel.text = timeLabel.text?.appendingFormat("%d", component.second!)
break
default:
break
}
}
错误:
0x108142301 <+9425>: callq 0x1082a95c4 ; symbol stub for: Foundation.Calendar.dateComponents (Swift.Set<Foundation.Calendar.Component>, from : Foundation.Date, to : Foundation.Date) -> Foundation.DateComponents
0x108142306 <+9430>: movq -0x710(%rbp), %rdi
0x10814230d <+9437>: movq %rax, -0x730(%rbp)
0x108142314 <+9444>: callq 0x10813d8a0 ; swift_rt_swift_release
0x108142319 <+9449>: movq -0x730(%rbp), %rax
0x108142320 <+9456>: movq %rax, -0x8(%rbp)
0x108142324 <+9460>: movq -0x218(%rbp), %rdi
0x10814232b <+9467>: callq 0x10813d8a0 ; swift_rt_swift_release
0x108142330 <+9472>: movq 0x1e3081(%rip), %rax ; (void *)0x000000010e87f068: swift_isaMask
0x108142337 <+9479>: movq -0x168(%rbp), %rdx
0x10814233e <+9486>: movq (%rdx), %rdi
0x108142341 <+9489>: andq (%rax), %rdi
0x108142344 <+9492>: movq %rdi, -0x738(%rbp)
0x10814234b <+9499>: movq %rdx, %rdi
0x10814234e <+9502>: movq -0x738(%rbp), %rax
0x108142355 <+9509>: callq *0x80(%rax)
0x10814235b <+9515>: movq %rax, -0x70(%rbp)
0x10814235f <+9519>: movq -0x230(%rbp), %rax
0x108142366 <+9526>: movsd -0x1d8(%rbp), %xmm0 ; xmm0 = mem[0],zero
0x10814236e <+9534>: movd %xmm0, %rax
0x108142373 <+9539>: cmpq $0x0, -0x70(%rbp)
0x108142378 <+9544>: jne 0x108142409 ; <+9689> at MainViewController.swift
0x10814237e <+9550>: movq -0x230(%rbp), %rax
0x108142385 <+9557>: movsd -0x1d8(%rbp), %xmm0 ; xmm0 = mem[0],zero
0x10814238d <+9565>: movd %xmm0, %rax
0x108142392 <+9570>: movq -0x230(%rbp), %rax
0x108142399 <+9577>: movsd -0x1d8(%rbp), %xmm0 ; xmm0 = mem[0],zero
0x1081423a1 <+9585>: movd %xmm0, %rax
0x1081423a6 <+9590>: movq -0x230(%rbp), %rax
0x1081423ad <+9597>: movsd -0x1d8(%rbp), %xmm0 ; xmm0 = mem[0],zero
0x1081423b5 <+9605>: movd %xmm0, %rax
0x1081423ba <+9610>: leaq 0x1b13f9(%rip), %rdi ; "fatal error"
0x1081423c1 <+9617>: movl $0xb, %eax
0x1081423c6 <+9622>: movl %eax, %esi
0x1081423c8 <+9624>: movl $0x2, %eax
0x1081423cd <+9629>: leaq 0x1b13ac(%rip), %rcx ; "unexpectedly found nil while unwrapping an Optional value"
0x1081423d4 <+9636>: movl $0x39, %edx
0x1081423d9 <+9641>: movl %edx, %r8d
0x1081423dc <+9644>: xorl %edx, %edx
0x1081423de <+9646>: movl %edx, -0x73c(%rbp)
0x1081423e4 <+9652>: movl %eax, %edx
0x1081423e6 <+9654>: movl %eax, %r9d
0x1081423e9 <+9657>: movl $0x0, (%rsp)
0x1081423f0 <+9664>: callq 0x1082a95f4 ; symbol stub for: function signature specialization <preserving fragile attribute, Arg[2] = Dead, Arg[3] = Dead> of Swift._fatalErrorMessage (Swift.StaticString, Swift.StaticString, file : Swift.StaticString, line : Swift.UInt, flags : Swift.UInt32) -> Swift.Never
0x1081423f5 <+9669>: movq -0x230(%rbp), %rcx
0x1081423fc <+9676>: movsd -0x1d8(%rbp), %xmm0 ; xmm0 = mem[0],zero
0x108142404 <+9684>: movd %xmm0, %rcx
0x108142409 <+9689>: leaq -0x70(%rbp), %rax
0x10814240d <+9693>: movq -0x70(%rbp), %rcx
0x108142411 <+9697>: movq %rcx, %rdi
0x108142414 <+9700>: movq %rax, -0x748(%rbp)
0x10814241b <+9707>: movq %rcx, -0x750(%rbp)
0x108142422 <+9714>: callq 0x1082a9858 ; symbol stub for: objc_retain
0x108142427 <+9719>: leaq 0x1b1398(%rip), %rdi ; "Seconds"
最佳答案
如果您去掉所有的 !
并用一些实际的错误检查替换它们,您应该能够很快找出错误发生的位置。例如,此版本会将出现的任何错误记录到控制台:
@IBAction func clickScreenBtn(_ sender: Any) {
guard let birthDay = UserDefaults.standard.object(forKey: "birthday") as? Date else {
print("Couldn't get birthday")
return
}
let currentDate = Date()
guard let day = Calendar.current.dateComponents([.day], from: birthDay, to: currentDate).day else {
print("Couldn't get day")
return
}
type = type + 1
switch type {
case 0:
unitLabel.text = "Days"
timeLabel.text = "\(day)"
case 4:
type = 0
unitLabel.text = "Days"
timeLabel.text = "\(day)"
case 1:
guard let hour = Calendar.current.dateComponents([.hour], from: birthDay, to: currentDate).hour else {
print("Couldn't get hour")
return
}
unitLabel.text = "Hours"
timeLabel.text = "\(hour)"
case 2:
guard let minute = Calendar.current.dateComponents([.minute], from: birthDay, to: currentDate).minute else {
print("Couldn't get minute")
return
}
unitLabel.text = "Minutes"
timeLabel.text = "\(minute)"
case 3:
guard let second = Calendar.current.dateComponents([.second], from: birthDay, to: currentDate).second else {
print("Couldn't get second")
return
}
unitLabel.text = "Seconds"
timeLabel.text = "\(second)"
default:
break
}
}
一旦您知道问题实际发生的位置,修复它就会容易得多。
关于ios - 如果年份小于 1948 年或大于 2086 年,为什么应用程序会崩溃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45909476/
我是一名优秀的程序员,十分优秀!