ios - 如何理解无意义的 Swift 堆栈跟踪?

标签 ios swift

从 Crashlytics 获取以下堆栈跟踪。

崩溃的代码是返回字符串

extension UserVisit: DashboardItem {

    public var dashboardItemAttributedTitle: NSAttributedString {
        let string = NSMutableAttributedString(string: "\(withUser.publicName) (\(withUser.publicAddress.fullDescription))", attributes: [NSFontAttributeName: UIFont.regularLight()])
        string.addAttribute(NSFontAttributeName, value: UIFont.regularBold(), range: NSMakeRange(0 , count(withUser.publicName)))
        return string  /*  Crash points to this line of code */
    }

}

代码指向返回语句。对于这次崩溃,我能想到的唯一原因是 string 为 nil,这是不可能的,因为如果它为 nil,则在调用 addAttribute 时会在上面的行中崩溃.遇到很多这样的崩溃,堆栈跟踪完全没有说明导致崩溃的原因

Thread : Crashed: com.apple.main-thread
0  Company                   0x00000001001c05c0 function signature specialization <Arg[0] = Owned To Guaranteed> of Company.UserVisit.dashboardItemAttributedTitle.getter : ObjectiveC.NSAttributedString (DashboardItemExtensions.swift:122)
1  Company                   0x00000001001be674 @objc Company.UserVisit.dashboardItemAttributedTitle.getter : ObjectiveC.NSAttributedString (DashboardItemExtensions.swift)
2  Company                   0x00000001001d3618 function signature specialization <Arg[0] = Owned To Guaranteed, Arg[1] = Owned To Guaranteed> of Company.DashboardItemCell.configure (Company.DashboardItemCell)(Company.DashboardItem) -> () (DashboardItemCell.swift:93)
3  Company                   0x00000001001d100c Company.DashboardItemCell.configure (Company.DashboardItemCell)(Company.DashboardItem) -> () (DashboardItemCell.swift)
4  Company                   0x0000000100163cb4 function signature specialization <Arg[0] = Owned To Guaranteed, Arg[1] = Owned To Guaranteed, Arg[2] = Owned To Guaranteed> of Company.DashboardViewController.collectionView (Company.DashboardViewController)(ObjectiveC.UICollectionView, cellForItemAtIndexPath : ObjectiveC.NSIndexPath) -> ObjectiveC.UICollectionViewCell (DashboardViewController.swift:567)
5  Company                   0x000000010015f3b8 @objc Company.DashboardViewController.collectionView (Company.DashboardViewController)(ObjectiveC.UICollectionView, cellForItemAtIndexPath : ObjectiveC.NSIndexPath) -> ObjectiveC.UICollectionViewCell (DashboardViewController.swift)
6  UIKit                          0x0000000187650710 -[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:] + 284
7  UIKit                          0x0000000187c7a850 __51-[UICollectionView _viewAnimationsForCurrentUpdate]_block_invoke1381 + 480
8  UIKit                          0x00000001877c5164 -[UICollectionView _viewAnimationsForCurrentUpdate] + 2892
9  UIKit                          0x0000000187c7b694 -[UICollectionView _updateWithItems:tentativelyForReordering:] + 1948
10 UIKit                          0x0000000187c79c30 -[UICollectionView _endItemAnimationsWithInvalidationContext:tentativelyForReordering:] + 10120
11 UIKit                          0x0000000187c7c988 -[UICollectionView _performBatchUpdates:completion:invalidationContext:tentativelyForReordering:] + 352
12 Company                   0x000000010016c214 Company.DashboardViewController.((fetchAndPopulateDashboard in _59C7F3AF672DD71404DED200B8F15814) (Company.DashboardViewController) -> () -> ()).(closure #1) (DashboardViewController.swift:370)
13 Company                   0x00000001000e1728 Company.BaseNetworkManager.(fetchObject (Company.BaseNetworkManager) -> <A>(A.Type, path : Swift.String, method : Company.Method, parameters : Swift.Optional<Swift.Dictionary<ObjectiveC.NSObject, Swift.AnyObject>>, errorHandler : Company.NetworkErrorHandler, completion : (Company.SingleNetworkResponse<A>) -> ()) -> Company.RequestMirror).(closure #1).(closure #1).(closure #1) (BaseNetworkManager.swift:219)
14 libdispatch.dylib              0x0000000194e6d994 _dispatch_call_block_and_release + 24
15 libdispatch.dylib              0x0000000194e6d954 _dispatch_client_callout + 16
16 libdispatch.dylib              0x0000000194e7220c _dispatch_main_queue_callback_4CF + 1608
17 CoreFoundation                 0x0000000182b2b7f8 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 12
18 CoreFoundation                 0x0000000182b298a0 __CFRunLoopRun + 1492
19 CoreFoundation                 0x0000000182a552d4 CFRunLoopRunSpecific + 396
20 GraphicsServices               0x000000018c4b36fc GSEventRunModal + 168
21 UIKit                          0x0000000187652f40 UIApplicationMain + 1488
22 Company                   0x000000010003ac1c main (main.m:13)
23 libdyld.dylib                  0x0000000194e9aa08 start + 4

最佳答案

属性字符串的范围必须采用 UTF-16 单位。

关于ios - 如何理解无意义的 Swift 堆栈跟踪?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31363012/

相关文章:

ios - 如何在后台刷新的并发症上显示 HealthKit 数据?

ios - 在 Swift 的另一个类中声明 self

swift - 在 NSView 上创建简单的动画循环

ios - react swift : How to write a Task Scheduler

ios - 如何在 iOS 中向 UIView 添加第二个 UITabBar( Controller ?)

ios - 发送 uitableviewcell 的索引路径

ios - 无法访问已安装的 Pod 框架

ios - Swift - 从 NavigationController 中的 UIViewController 到 TabBarController 中的 UIViewController

ios - 错误线程 1 : Fatal error: Unexpectedly found nil while unwrapping an Optional value?

swift - UIAlertController --> 用于更新变量?