UICollectionReusableView – 缺less函数返回

考虑到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") } } 

所有这些在即时部署到模拟器或真实设备时都可以很好地工作,但奇怪的是,当我想为testing目的构buildAd-Hoc包时,它告诉我

缺less返回的函数预期返回'UICollectionReusableView'

那么好吧,switch-case之外什么也没有,所以它什么都不能返回 – 但是为什么只有在我试图构build一个包的时候才会给出“热部署”的警告呢?

assert()仅在Debugconfiguration中评估。 当你build立一个档案时,代码被编译成发布configuration(带有优化),条件被简单地忽略(假定为true )。 因此编译器会抱怨缺less的返回值。

您可以使用

 fatalError("Unexpected element kind") 

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

另请参阅Swift – 带开关语句的fatalError 。