【问题标题】:Crash in line where should be not crash in Swift ("if, let, where" statement)在 Swift 中不应该在 where 中崩溃(“if, let, where” 语句)
【发布时间】:2016-09-17 11:10:24
【问题描述】:

我一直在与一个根本不应该发生的非常奇怪的崩溃作斗争。我通过 Hockeyapp 收到崩溃报告,它不断报告应用程序根本不应该崩溃的行中的崩溃。我已经面临这个问题 1 周了。

这是崩溃报告

0   TeacherBox                           0x00000001000864f0 TeacherBox.RequestLessonViewController.loadExistingRequests () -> () (RequestLessonViewController.swift:755)
1   TeacherBox                           0x0000000100086514 @objc TeacherBox.RequestLessonViewController.loadExistingRequests () -> () (RequestLessonViewController.swift:0)
2   TeacherBox                           0x0000000100086a84 function signature specialization <Arg[0] = Dead> of TeacherBox.RequestLessonViewController.viewDidAppear (Swift.Bool) -> () (RequestLessonViewController.swift:122)
3   TeacherBox                           0x000000010007e570 @objc TeacherBox.RequestLessonViewController.viewDidAppear (Swift.Bool) -> () (RequestLessonViewController.swift:0)
4   UIKit                                0x00000001895b84dc -[UIViewController _setViewAppearState:isAnimating:] + 844
5   UIKit                                0x00000001895b8a40 -[UIViewController _endAppearanceTransition:] + 216
6   UIKit                                0x0000000189671038 -[UINavigationController navigationTransitionView:didEndTransition:fromView:toView:] + 1232
7   UIKit                                0x0000000189742198 __49-[UINavigationController _startCustomTransition:]_block_invoke + 228
8   UIKit                                0x00000001896c7cc4 -[_UIViewControllerTransitionContext completeTransition:] + 112
9   UIKit                                0x00000001898181ec __53-[_UINavigationParallaxTransition animateTransition:]_block_invoke.97 + 708
10  UIKit                                0x00000001895d9214 -[UIViewAnimationBlockDelegate _didEndBlockAnimation:finished:context:] + 488
11  UIKit                                0x00000001895d8d38 -[UIViewAnimationState sendDelegateAnimationDidStop:finished:] + 308
12  UIKit                                0x00000001895d8b78 -[UIViewAnimationState animationDidStop:finished:] + 156

代码行数:

if let booking = rescheduleBooking where booking.confirmed! == 0 {
    existingRequests.append(booking)
}

第 755 行是 if 语句。在 swift 的“if, let, where”语句中,“let”检查是否存在 rescheduleBooking,如果存在并已分配,则执行 where 语句......我是对的吗?......无论如何,我测试了在我的设备和模拟器中本地并且它不会在那里崩溃,无论变量的值是什么......它发生在我手头没有的不同设备中......

如果您有任何建议,或者如果我没有正确理解“if, let where”子句,我将非常感谢您的帮助和 cmets。

谢谢..

【问题讨论】:

  • 删除 let 并尝试。
  • 如果booking.confirmed 为 nil 将会崩溃,因为你已经强制解包了

标签: ios swift crash hockeyapp


【解决方案1】:

您对“if-let where”子句的理解是正确的,但您必须牢记if let booking = rescheduleBooking 确保rescheduleBooking 不是nil,然后将其分配给booking。现在即使 booking 不是 nil 也不能保证 booking.confirmed 不会是 nil 所以如果它是 nil 并且你强制解开它会导致崩溃。

您可以添加另一个 let 以确保 booking.confirmed 不是 nil

if let booking = rescheduleBooking, let bookingConfirmed = booking.confirmed where bookingConfirmed == 0 {
    existingRequests.append(booking)
}

【讨论】:

  • 嗨,感谢您的回答.. 现在确认不再是可选的,但它继续在那里崩溃...
  • 我们应该从崩溃中与您的代码相关的最低行开始,即3 TeacherBox 0x000000010007e570 @objc TeacherBox.RequestLessonViewController.viewDidAppear (Swift.Bool) -&gt; () (RequestLessonViewController.swift:0) 。能否请您包含该行中的相关代码?
  • 如果您可以的话,请提供完整的崩溃报告以及崩溃报告中第 2 行和第 1 行的相关代码,这样我就可以看到发生了什么以找出原因崩溃。
  • 您还可以设置断点以找出导致崩溃的确切行
  • 所有这些崩溃都发生在其中一位测试人员的智能手机中,而不是其他人...我决定在他的 Mac 上安装 XCode,连接该 iPhone 并直接查看发生了什么(通过 teamviewer 原因' 他现在在另一个国家)...我会在这里发布解决方案...我真的认为 HockeyApp 的崩溃报告有一些错误,或者至少没有指向正确的行..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-02
  • 1970-01-01
  • 1970-01-01
  • 2016-02-02
  • 1970-01-01
相关资源
最近更新 更多