UICollectionReusableView - 函数中缺少返回 [英] UICollectionReusableView - Missing return in a function

查看:34
本文介绍了UICollectionReusableView - 函数中缺少返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I had a weird problem running into considering a header of a UICollectionView.

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

I basically used the code from: 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 包进行测试时,它告诉我

All that works perfectly fine when instantly deploying to either the simulator or a real device, but oddly when I wanna build an Ad-Hoc Package for testing purposes it tells me

预期返回UICollectionReusableView"的函数中缺少返回

Missing return in a function expected to return 'UICollectionReusableView'

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

Ok so far so good, there is nothing outside the switch-case so it could return nothing - but why does it not give any warnings on "hot deploy" only when I try to build a package?

推荐答案

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

assert() is evaluated only in the Debug configuration. When you build an archive then the code is compiled in the Release configuration (with optimizations) and the condition is simply ignored (assumed to be true). Therefore the compiler complains about the missing return value.

你可以使用

fatalError("Unexpected element kind")

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

instead. fatalError() is always evaluated and in addition marked with @noreturn (resp. return type Never in Swift 3) so that the compiler knows that it does not return to its caller.

另请参阅 Swift - fatalError with Switch 语句.

这篇关于UICollectionReusableView - 函数中缺少返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆