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

查看:719
本文介绍了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'

好到目前为止一切都很好,在开关盒之外什么都没有,所以它什么都不返回 - 但为什么它没有给出任何警告热部署只有当我尝试构建一个包?

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()仅在Debug配置中计算。当您构建
存档时,代码将在Release配置
(带优化)中编译,并且条件被忽略(假设
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 标记
(resp。返回类型<$在Swift 3中c $ c>从不,以便编译器知道它不会返回其调用者。

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

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

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