ios - UICollectionView cellForItemAtIndexPath 上的 Swift 专门崩溃

标签 ios swift swift2 uicollectionview ios9

Crashlytics 为我提供了以下堆栈跟踪。 崩溃发生不一致。发生在所有 iOS 9 设备上,但很少见。无法找出问题的根源。 在我拥有的任何设备上都没有发生过,过去 3 天一直试图崩溃。

Crashed: com.apple.main-thread
0  cherish                        0x10014ee18 specialized PersonalizeViewController.collectionView(UICollectionView, cellForItemAtIndexPath : NSIndexPath) -> UICollectionViewCell (PersonalizeViewController.swift:159)
1  cherish                        0x1001497f0 @objc PersonalizeViewController.collectionView(UICollectionView, cellForItemAtIndexPath : NSIndexPath) -> UICollectionViewCell (PersonalizeViewController.swift)
2  UIKit                          0x188aef3a8 <redacted> + 432
3  UIKit                          0x188311adc <redacted> + 4628
4  UIKit                          0x18830c808 <redacted> + 228
5  UIKit                          0x1882a81e4 <redacted> + 656
6  QuartzCore                     0x185c3a994 <redacted> + 148
7  QuartzCore                     0x185c355d0 <redacted> + 292
8  QuartzCore                     0x185c35490 <redacted> + 32
9  QuartzCore                     0x185c34ac0 <redacted> + 252
10 QuartzCore                     0x185c34820 <redacted> + 500
11 QuartzCore                     0x185c2dde4 <redacted> + 80
12 CoreFoundation                 0x183104728 <redacted> + 32
13 CoreFoundation                 0x1831024cc <redacted> + 372
14 CoreFoundation                 0x1831028fc <redacted> + 928
15 CoreFoundation                 0x18302cc50 CFRunLoopRunSpecific + 384
16 GraphicsServices               0x184914088 GSEventRunModal + 180
17 UIKit                          0x188316088 UIApplicationMain + 204
18 cherish                        0x100142a50 main (AppDelegate.swift:19)
19 libdispatch.dylib              0x182bca8b8 (Missing)

崩溃的代码是:

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        if collectionView.tag == 1 {
            (---crash line---) let cell = select_date_collection_view.dequeueReusableCellWithReuseIdentifier(PERSONALIZE_SELECT_DATE_COLLECTION_CELL_IDENTIFIER, forIndexPath: indexPath) as! SelectDateCollectionViewCell 
            // Some changes to cell objects
            return cell
        } else if collectionView.tag == 2 {
            let cell = select_time_collection_view.dequeueReusableCellWithReuseIdentifier(PERSONALIZE_SELECT_TIME_COLLECTION_CELL_IDENTIFIER, forIndexPath: indexPath) as! SelectTimeCollectionViewCell
            // Some changes to cell objects
            return cell
        } else if collectionView.tag == 3 {
            let cell = add_customization_collection_view.dequeueReusableCellWithReuseIdentifier(PERSONALIZE_ADD_CUSTOMIZATION_COLLECTION_CELL_IDENTIFIER, forIndexPath: indexPath) as! AddCustomizationCollectionViewCell
            // Some changes to cell objects
            return cell
        }

        let cell: UICollectionViewCell = UICollectionViewCell()
        return cell
    }

已经在viewDidLoad()中初始化了所有的collection view

        select_date_collection_view.registerNib(UINib(nibName: "SelectDateCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: PERSONALIZE_SELECT_DATE_COLLECTION_CELL_IDENTIFIER)
        select_date_collection_view.tag = 1
        select_date_collection_view.dataSource = self
        select_date_collection_view.delegate = self
        select_date_collection_view.showsHorizontalScrollIndicator = false
        select_date_collection_view.showsVerticalScrollIndicator = false
        select_date_collection_view.reloadData()

        select_time_collection_view.registerNib(UINib(nibName: "SelectTimeCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: PERSONALIZE_SELECT_TIME_COLLECTION_CELL_IDENTIFIER)
        select_time_collection_view.tag = 2
        select_time_collection_view.dataSource = self
        select_time_collection_view.delegate = self
        select_time_collection_view.showsHorizontalScrollIndicator = false
        select_time_collection_view.showsVerticalScrollIndicator = false
        hideSelectTimeView()

        add_customization_collection_view.registerNib(UINib(nibName: "AddCustomizationCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: PERSONALIZE_ADD_CUSTOMIZATION_COLLECTION_CELL_IDENTIFIER)
        add_customization_collection_view.tag = 3
        add_customization_collection_view.dataSource = self
        add_customization_collection_view.delegate = self
        add_customization_collection_view.showsHorizontalScrollIndicator = false
        add_customization_collection_view.showsVerticalScrollIndicator = false
        hideCustomizationsView()

错误发生不一致,这是引起关注的主要原因。因为我无法找出问题所在,而且 itunes 或 Crashlytics 的崩溃日志也无济于事。

最佳答案

我终于拿到了一部崩溃的 iPhone。 结果发现有几个问题:

iOS、Crashlytics 和 iTunes 的崩溃报告完全没有用。代码崩溃的实际位置从来没有被他们标记过,而是在 Debug模式下崩溃时被 xcode 标记过。所以不要相信 crashlytics 或 itunes 生成的报告。

实际的问题是我从服务器获得了“HH:mm:ss”格式的时间,我正在将其转换为“hh:mm a”格式,因为我想要我的 UI 的 AM、PM。现在 iOS 所做的一件好事是,如果用户的手机时间设置为 24 小时格式,则不会返回 AM/PM 并且应用程序会崩溃,因为我没有检查返回的字符串是否为 nil。为了解决上述问题,我必须将语言环境设置为:

df.locale = NSLocale(localeIdentifier: "en_US_POSIX")

问题终于解决了。 主要学习是不要依赖crashlytics或者itunes报告

关于ios - UICollectionView cellForItemAtIndexPath 上的 Swift 专门崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37276942/

相关文章:

ios - 将非 App Store iOS 应用程序转移到另一个 iOS 企业开发者帐户

ios - 如何在 swift 中使用实体关系从核心数据中获取数据?

ios - 使用 Swift 的 Firebase

ios - 可点击的 UITableView 部分

ios - 将 Base64 字符串应用于 UIImageView 在 iOS Swift 2 中不起作用

iphone - 如何在 iOS 中读取音频文件并获取样本?

objective-c - iOS 获取连接

ios - 刷新 NSFetchedResultsController 数据?

ios - Swift 2 : AnyObject? 不可转换为 NSString

ios - 如何使用多个数组设置 for 循环作为变量以用作 Swift 2 中标记中的文本