【问题标题】:UICollectionReusableView - Missing return in a functionUICollectionReusableView - 函数中缺少返回
【发布时间】:2015-05-22 19:55:12
【问题描述】:

在考虑 UICollectionView 的标头时遇到了一个奇怪的问题。

我基本上使用了以下代码: http://www.raywenderlich.com/78551/beginning-ios-collection-views-swift-part-2

func collectionView(collectionView: UICollectionView,
        viewForSupplementaryElementOfKind kind: String,
        atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {

            let dateFormatter = NSDateFormatter()
            dateFormatter.dateFormat = "dd.MM.yyyy' - 'HH:mm'"
            //1
            switch kind {
                //2
            case UICollectionElementKindSectionHeader:
                //3
                let h =
                collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: "eventHeaderView", forIndexPath: indexPath) as eventHeader


                h.eventFirstline.text = "First Line"
                h.eventSecondline.text = thisEvent.eventName

                h.eventDate.text = dateFormatter.stringFromDate(thisEvent.startDate)

                h.eventDescription.text = thisEvent.shortDescription

                return h
            default:
                //4
                assert(false, "Unexpected element kind")
            }
    }

当立即部署到模拟器或真实设备时,所有这些都可以正常工作,但奇怪的是,当我想构建一个 Ad-Hoc 包进行测试时,它告诉我

预期返回“UICollectionReusableView”的函数中缺少返回

好吧,到目前为止一切都很好,在 switch-case 之外没有任何东西,所以它什么也不会返回 - 但是为什么只有在我尝试构建包时它才对“热部署”发出任何警告?

【问题讨论】:

    标签: ios swift uicollectionview uicollectionreusableview


    【解决方案1】:

    assert() 仅在调试配置中进行评估。当你建立 存档,然后代码在发布配置中编译 (带有优化)并且条件被简单地忽略(假设 是true)。因此编译器抱怨缺少 返回值。

    你可以使用

    fatalError("Unexpected element kind")
    

    相反。 fatalError() 总是被评估并被标记 使用 @noreturn(在 Swift 3 中分别返回类型为 Never),以便编译器知道它不会返回给调用者。

    另见Swift - fatalError with Switch Statements

    【讨论】:

    • 你无处不在!感谢您的另一个答案。 :)
    猜你喜欢
    • 2016-10-24
    • 1970-01-01
    • 2019-05-14
    • 2018-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多