最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】swift代码运行出错:Thread 1 signal SIGABRT

Swift crifan 12241浏览 0评论

【背景】

折腾:

【已解决】swift添加tab页面出错:Cannot convert value of type UITabBarController to expected argument type UIView

期间,把代码改为了:

self.view.addSubview(recordTabBarView.view)

结果运行时:

点击到对应的页面时:

click one cell in uitableview of swift

 

程序停止运行了,出现错误:

Thread 1: signal SIGABRT

对应的日志:

libc++abi.dylib: terminating with uncaught exception of type NSException

如图:

ios app Thread 1 signal SIGABRT terminating with uncaught exception of type NSException

 

【解决过程】

1.搜:

swift Thread 1 signal SIGABRT

参考:

ios – Swift: Thread 1 signal SIGABRT – Stack Overflow

How to fix thread 1 signal SIGABRT in Swift Xcode – YouTube

去看看:

launchscreen xib outlet collections

此处很明显:

connection inspector中,没有找到我们要的,错误的原因:

因为此处根本没有什么outlet之类的东西

我们此处的UI都是代码内部写的,没有用到这部分的内容。

 

2.另外接着看看:

好像此处是缺少了对于代码中的 UITabBarController的其他某种元素?

进去看看:

into UITabBarControllerdelegate source code

好像是:

虽然用了:UITabBarController

但是貌似是不是对于:

UITabBarControllerDelegate

也要去继承的?

 

3.去搜:

swift UITabBarController UITabBarControllerDelegate

参考:

UITabBarControllerDelegate Protocol Reference

说是:

所有的方法都是可选的

-》看起来,对于使用了UITabBarController后,未必一定要使用UITabBarControllerDelegate

-》如果没有特殊的设计,就用默认的UITabBarController就好了。

 

4.参考:

ios – How and where to initialize UITabBarControllerDelegate in Swift? – Stack Overflow

去试试,把当前自己设置为delegate:

recordTabBarView.delegate = self

看看效果如何:

结果没法设置,会报错:

Cannot assign a value of type ‘MySchoolCoinViewController’ to a value of type ‘UITabBarControllerDelegate?’

5.后来自己重新调试代码,发现执行到这一行:

        recordTabBarView.viewControllers = tabControllers

时才出现:

Thread 1: signal SIGABRT

的错误的。

6.后来经过自己的摸索,终于找到一点点线索了:

在log输出的地方,是可以找到,异常时候的log的:

此处刚出错的时候的log是:

2015-10-04 20:30:05.157 xxx[12834:1456266] *** Terminating app due to uncaught exception ‘NSInternalInconsistencyException’, reason: ‘Could not load NIB in bundle: ‘NSBundle </Users/crifan/Library/Developer/CoreSimulator/Devices/FBA9D3B3-5FF3-4673-9C78-7A78809E7574/data/Containers/Bundle/Application/16A9CE53-9C8B-4083-9CE5-A0BCD8B635DD/xxx.app> (loaded)’ with name ‘全部记录

log  Terminating app due to uncaught exception NSInternalInconsistencyException

对应的内容稍经分析即可看出:

此处给出了错误的原因

给出了稍微详细点的错误的细节和具体所涉及哪里的信息:

with name ‘全部记录”

-》可以去代码中搜:全部记录

-》然后容易找到相关部分的代码

search error text in project find related files

-》利于分析,到底是哪里出错的了

点击到对应的文件中,自己分析自己的代码

看看是哪里的问题

-》至少比无头苍蝇的,找不到任何的错误相关的信息,要好多了。

 

另外:

仔细再去看其中的错误的log:

*** First throw call stack:
(
0   CoreFoundation              0x0000000110c65f65 __exceptionPreprocess + 165
1   libobjc.A.dylib             0x0000000112bdcdeb objc_exception_throw + 48
2   CoreFoundation              0x0000000110c65e9d +[NSException raise:format:] + 205
3   UIKit                       0x0000000111b03db1 -[UINib instantiateWithOwner:options:] + 499
4   UIKit                       0x00000001118ef84d -[UIViewController _loadViewFromNibNamed:bundle:] + 381
5   UIKit                       0x00000001118f0179 -[UIViewController loadView] + 178
6   UIKit                       0x00000001118f047c -[UIViewController loadViewIfRequired] + 139
7   UIKit                       0x00000001118f0c7d -[UIViewController view] + 27
8   UIKit                       0x00000001119506db -[UITabBarController transitionFromViewController:toViewController:transition:shouldSetSelected:] + 502
9   UIKit                       0x000000011194fa35 -[UITabBarController transitionFromViewController:toViewController:] + 59
10  UIKit                       0x000000011194b87d -[UITabBarController _setSelectedViewController:] + 377
11  UIKit                       0x000000011194b5f6 -[UITabBarController setSelectedIndex:] + 131
12  UIKit                       0x000000011180aeec +[UIView(Animation) performWithoutAnimation:] + 65
13  UIKit                       0x000000011194aff4 -[UITabBarController _setViewControllers:animated:] + 3427
14  UIKit                       0x000000011194b1b3 -[UITabBarController setViewControllers:animated:] + 119
15  xxxxxx                  0x000000010fcd64e1 _TFC10xxxxxx26MySchoolCoinViewController11viewDidLoadfS0_FT_T_ + 7937
16  xxxxxx                  0x000000010fcd6792 _TToFC10xxxxxx26MySchoolCoinViewController11viewDidLoadfS0_FT_T_ + 34
17  UIKit                       0x00000001118f0931 -[UIViewController loadViewIfRequired] + 1344
18  UIKit                       0x00000001118f6923 -[UIViewController __viewWillAppear:] + 120
19  UIKit                       0x000000011192618a -[UINavigationController _startCustomTransition:] + 1177
20  UIKit                       0x00000001119357c7 -[UINavigationController _startDeferredTransitionIfNeeded:] + 712
21  UIKit                       0x000000011193667d -[UINavigationController __viewWillLayoutSubviews] + 57
22  UIKit                       0x0000000111ace63d -[UILayoutContainerView layoutSubviews] + 248
23  UIKit                       0x000000011ea1faac -[UILayoutContainerViewAccessibility layoutSubviews] + 43
24  UIKit                       0x000000011181611c -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 710
25  QuartzCore                  0x000000011021936a -[CALayer layoutSublayers] + 146
26  QuartzCore                  0x000000011020dbd0 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 366
27  QuartzCore                  0x000000011020da4e _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
28  QuartzCore                  0x00000001102021d5 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 277
29  QuartzCore                  0x000000011022f9f0 _ZN2CA11Transaction6commitEv + 508
30  UIKit                       0x000000011178f53a _afterCACommitHandler + 174
31  CoreFoundation              0x0000000110b919d7 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
32  CoreFoundation              0x0000000110b91947 __CFRunLoopDoObservers + 391
33  CoreFoundation              0x0000000110b8759b __CFRunLoopRun + 1147
34  CoreFoundation              0x0000000110b86e98 CFRunLoopRunSpecific + 488
35  GraphicsServices            0x00000001148d4ad2 GSEventRunModal + 161
36  UIKit                       0x0000000111765676 UIApplicationMain + 171
37  xxxxxx                  0x000000010fd3be3d main + 109
38  libdyld.dylib               0x0000000113deb92d start + 1
39  ???                         0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

其中有:

_TFC10SchoolLike26MySchoolCoinViewController11viewDidLoadfS0_FT_T

很容易看出:

就是Swift编译后的代码,类似于C++的编译后的代码,的函数的命名方式

-》可以基本上看出,具体对应的是:

xxx-》MySchoolCoinViewController-》viewDidLoad

-》此时就很明显了:

这个就是对应的出了错误的代码的所在位置啊:

去项目导航中找到对应的文件和函数:

find error function MySchoolCoinViewController viewDidLoad

 

而此处,实际上由于自己的调试而得之,出错的代码,的确就是:

MySchoolCoinViewController.swift中的viewDidLoad函数

中的:

recordTabBarView.viewControllers = tabControllers

 

7.找到出错的位置后,就可以去尝试解决了。

然后经过修改代码:

AllRecordTabViewController.swift

import UIKit

class AllRecordTabViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
       
        view.backgroundColor = UIColor.greenColor()
        self.title = "全部记录"
    }
   
}

另外的:

IncomeRecordTabViewController.swift

ExpenseRecordTabViewController.swift

都是类似的,都在viewDidLoad中有:

self.title = "xx记录"

然后对于调用者:

        let recordTabBarView = UITabBarController()
        let allRecordTab = AllRecordTabViewController(nibName: "全部记录", bundle: nil)
        let incomeRecordTab = IncomeRecordTabViewController(nibName: "收入记录", bundle: nil)
        let expenseRecordTab = ExpenseRecordTabViewController(nibName: "支出记录", bundle: nil)
        let tabControllers = [allRecordTab, incomeRecordTab, expenseRecordTab]
       
        recordTabBarView.viewControllers = tabControllers
        recordTabBarView.selectedIndex = 0

为:

        let recordTabBarView = UITabBarController()
        //let allRecordTab = AllRecordTabViewController(nibName: "全部记录", bundle: nil)
        let allRecordTab = AllRecordTabViewController()
        //let incomeRecordTab = IncomeRecordTabViewController(nibName: "收入记录", bundle: nil)
        let incomeRecordTab = IncomeRecordTabViewController()
        //let expenseRecordTab = ExpenseRecordTabViewController(nibName: "支出记录", bundle: nil)
        let expenseRecordTab = ExpenseRecordTabViewController()
        let tabControllers = [allRecordTab, incomeRecordTab, expenseRecordTab]
       
        recordTabBarView.viewControllers = tabControllers
        recordTabBarView.selectedIndex = 0

即可正常运行,通过之前出错的代码了。

after fix error code debug can continue run

 

就真正完全的解决问题了。

 

【总结】

对于当iOS中的app运行出现:

Thread 1: signal SIGABRT

的错误时,想要找到具体是哪里出了错,则可以:

1.通过Xcode-》console,即输出print的log信息的地方

可以找到出错的那一刻打印的信息,类似于这样:

2015-10-04 20:30:05.157 xxx[12834:1456266] *** Terminating app due to uncaught exception ‘NSInternalInconsistencyException’, reason: ‘Could not load NIB in bundle: ‘NSBundle </Users/crifan/Library/Developer/CoreSimulator/Devices/FBA9D3B3-5FF3-4673-9C78-7A78809E7574/data/Containers/Bundle/Application/16A9CE53-9C8B-4083-9CE5-A0BCD8B635DD/xxx.app> (loaded)’ with name ‘全部记录”

*** First throw call stack:

其中的:

with name ‘全部记录”

就给出了,错误的相关信息

-》所以我们就可以去搜索对应的:全部记录

而找到相关的文件

-》然后自己去分析,到底是哪些文件是可能出错的文件

-》从而进一步分析,到底是哪里,哪个函数,哪部分的代码,出了错。

2.可以通过console的信息中,定位分析到

出错的类(文件)和函数

比如此处log信息中就有类似的出错信息:

13  UIKit      0x000000011194aff4 -[UITabBarController _setViewControllers:animated:] + 3427
14  UIKit      0x000000011194b1b3 -[UITabBarController setViewControllers:animated:] + 119
15  SchoolLike 0x000000010fcd64e1 _TFC10xxxxxx26MySchoolCoinViewController11viewDidLoadfS0_FT_T_ + 7937
16  SchoolLike 0x000000010fcd6792 _TToFC10SchoolLike26MySchoolCoinViewController11viewDidLoadfS0_FT_T_ + 34
17  UIKit      0x00000001118f0931 -[UIViewController loadViewIfRequired] + 1344

而其中的:

_TFC10xxxxxx26MySchoolCoinViewController11viewDidLoadfS0_FT_T_

把中间的数据和其他的字符去掉,就可以看出来是:

xxxxxx-》MySchoolCoinViewController-》viewDidLoad

从而得知是:

MySchoolCoinViewController.swift中的viewDidLoad函数

出了错。

3.从以上的

错误相关信息的搜索

加上:

准确定位到出错的类文件的所在函数的位置

以及自己对自己的代码的了解

往往就可以:

定位到代码出错的具体的位置,然后一点点修改代码,调试代码,解决问题了。

 

总之:

xcode的调试功能,不是一般的垃圾

-》真不知道mac下的之前的程序员,都是怎么忍受的xcode的。。。

-》相比之下,Windows下面的用Visual Studio去调试错误,简直都是享受啊~~~~

转载请注明:在路上 » 【已解决】swift代码运行出错:Thread 1 signal SIGABRT

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
84 queries in 0.194 seconds, using 22.09MB memory