崩溃:com.apple.main-thread - 应用程序在 guard 语句上随机崩溃
Crashed: com.apple.main-thread - App crashes randomly on guard statement
我在实时应用程序中随机崩溃。它在警卫声明中崩溃。以下是代码片段。我不明白为什么它会在本应防止这样的崩溃的守卫中崩溃?
如何解决这个问题或我该如何着手调查它?
Crashlytics 日志
Crashed: com.apple.main-thread
EXC_BREAKPOINT 0x0000000102419850
0 myApp 0x102426f50 MessageVC.validate() + 101 (MessageVC.swift:101)
1 myApp 0x1024272d8 @objc MessageVC.buttonTapped(_:) + 239 (MessageVC.swift:239)
2 UIKitCore 0x1a6a8d9ac <redacted> + 96
3 UIKitCore 0x1a64c3fbc <redacted> + 240
4 UIKitCore 0x1a64c4320 <redacted> + 408
5 UIKitCore 0x1a64c333c <redacted> + 520
6 UIKitCore 0x1a6674a58 <redacted> + 7636
7 CoreFoundation 0x1a2983e68 <redacted> + 32
8 CoreFoundation 0x1a297ed54 <redacted> + 416
9 CoreFoundation 0x1a297f320 <redacted> + 1308
10 CoreFoundation 0x1a297eadc CFRunLoopRunSpecific + 464
11 GraphicsServices 0x1ac91f328 GSEventRunModal + 104
12 UIKitCore 0x1a6a8c63c UIApplicationMain + 1936
13 myApp 0x1023eb850 main + 21 (ProfileVC.swift:11)
14 libdyld.dylib 0x1a2808360 <redacted> + 4
Model.swift
struct ProfileStatus: Decodable {
var status: Bool?
var error: String?
}
MessageVC.swift
var profileStatus: ProfileStatus!
func validate() -> Bool {
guard let status = profileStatus.status else { // Line no. 101, Crash here
return true
}
// Do something...
}
@IBAction func buttonTapped(_ sender: UIButton) {
if validate() { // Line no. 239
// Do something..
}
}
ProfileVC.swift
class ProfileVC: UIViewController {
@IBOutlet weak var textField: MyTextField! // Line no. 11
// ....
}
强制解包崩溃的原因
var profileStatus: ProfileStatus! // << here is the reason
所以你需要在其他代码中找到丢失所有权的地方,而且,无论如何这是一个好习惯
var profileStatus: ProfileStatus? // use optional and unwrap where needed conditonally
func validate() -> Bool {
guard let status = profileStatus?.status else {
我在实时应用程序中随机崩溃。它在警卫声明中崩溃。以下是代码片段。我不明白为什么它会在本应防止这样的崩溃的守卫中崩溃?
如何解决这个问题或我该如何着手调查它?
Crashlytics 日志
Crashed: com.apple.main-thread
EXC_BREAKPOINT 0x0000000102419850
0 myApp 0x102426f50 MessageVC.validate() + 101 (MessageVC.swift:101)
1 myApp 0x1024272d8 @objc MessageVC.buttonTapped(_:) + 239 (MessageVC.swift:239)
2 UIKitCore 0x1a6a8d9ac <redacted> + 96
3 UIKitCore 0x1a64c3fbc <redacted> + 240
4 UIKitCore 0x1a64c4320 <redacted> + 408
5 UIKitCore 0x1a64c333c <redacted> + 520
6 UIKitCore 0x1a6674a58 <redacted> + 7636
7 CoreFoundation 0x1a2983e68 <redacted> + 32
8 CoreFoundation 0x1a297ed54 <redacted> + 416
9 CoreFoundation 0x1a297f320 <redacted> + 1308
10 CoreFoundation 0x1a297eadc CFRunLoopRunSpecific + 464
11 GraphicsServices 0x1ac91f328 GSEventRunModal + 104
12 UIKitCore 0x1a6a8c63c UIApplicationMain + 1936
13 myApp 0x1023eb850 main + 21 (ProfileVC.swift:11)
14 libdyld.dylib 0x1a2808360 <redacted> + 4
Model.swift
struct ProfileStatus: Decodable {
var status: Bool?
var error: String?
}
MessageVC.swift
var profileStatus: ProfileStatus!
func validate() -> Bool {
guard let status = profileStatus.status else { // Line no. 101, Crash here
return true
}
// Do something...
}
@IBAction func buttonTapped(_ sender: UIButton) {
if validate() { // Line no. 239
// Do something..
}
}
ProfileVC.swift
class ProfileVC: UIViewController {
@IBOutlet weak var textField: MyTextField! // Line no. 11
// ....
}
强制解包崩溃的原因
var profileStatus: ProfileStatus! // << here is the reason
所以你需要在其他代码中找到丢失所有权的地方,而且,无论如何这是一个好习惯
var profileStatus: ProfileStatus? // use optional and unwrap where needed conditonally
func validate() -> Bool {
guard let status = profileStatus?.status else {