UICollectionReusableView - 函数中缺少返回

lon*_*bow 14 ios uicollectionview uicollectionreusableview swift

考虑到标题,我遇到了一个奇怪的问题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")
            }
    }
Run Code Online (Sandbox Code Playgroud)

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

在期望返回'UICollectionReusableView'的函数中缺少返回

好的,到目前为止一切都很好,切换机箱外没有任何东西,所以它什么都不返回 - 但是为什么它只在我试图建立一个包时才给出"热部署"的任何警告?

Mar*_*n R 48

assert()仅在Debug配置中计算.构建存档时,代码将在Release配置中进行编译(带优化),并且简单地忽略条件(假设为true).因此编译器会抱怨缺少返回值.

您可以使用

fatalError("Unexpected element kind")
Run Code Online (Sandbox Code Playgroud)

代替.fatalError()总是被评估,并另外标记@noreturn(Never在Swift 3中的resp.返回类型),以便编译器知道它不会返回给它的调用者.

另请参见Swift - 带有切换语句的fatalError.