UICollectionReusableView - Missing return in a function

longbow

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

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")
            }
    }

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

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?

Martin R

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.

You can use

fatalError("Unexpected element kind")

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.

See also Swift - fatalError with Switch Statements.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Recursive Function Missing Return Statement

From Dev

Missing return in a function expected to return 'UIImage'

From Dev

Swift switch default + fallthrough: missing return in a function expected to return 'String'

From Dev

instaFilter Processor error - missing return in a function expected to return

From Dev

instaFilter Processor error - missing return in a function expected to return

From Java

Typescript | Warning about Missing Return Type of function, ESLint

From Dev

When a function missing the return value, the compiler generates a warning but not an error?

From Dev

Why does it work when a function missing a return statement at the end?

From Dev

When a function missing the return value, the compiler generates a warning but not an error?

From Dev

Missing return in a function expected to return 'Bool' parse.com push notification

From Dev

Missing return address on stack

From Dev

PHPStorm missing return statement

From Dev

Missing return statement in for loop

From Dev

AsyncTask missing return statement

From Dev

Missing return UITableViewCell

From Dev

Missing return statement with switch

From Dev

Two Missing Return Statements

From Dev

Missing return statement in PhpStorm

From Dev

Return array missing values

From Dev

PHPStorm missing return statement

From Dev

Missing return statement

From Dev

Missing return statement in for loop

From Dev

Missing return statement with for loop

From Dev

AsyncTask missing return statement

From Dev

Missing a return statement somewhere?

From Dev

Missing return statements, loops

From Dev

Missing return in Swift

From Dev

I have a function returning a vector of strings. It takes two strings and is to return the strings in one that are missing in the other

From Dev

UICollectionReusableView for section header not working

Related Related

  1. 1

    Recursive Function Missing Return Statement

  2. 2

    Missing return in a function expected to return 'UIImage'

  3. 3

    Swift switch default + fallthrough: missing return in a function expected to return 'String'

  4. 4

    instaFilter Processor error - missing return in a function expected to return

  5. 5

    instaFilter Processor error - missing return in a function expected to return

  6. 6

    Typescript | Warning about Missing Return Type of function, ESLint

  7. 7

    When a function missing the return value, the compiler generates a warning but not an error?

  8. 8

    Why does it work when a function missing a return statement at the end?

  9. 9

    When a function missing the return value, the compiler generates a warning but not an error?

  10. 10

    Missing return in a function expected to return 'Bool' parse.com push notification

  11. 11

    Missing return address on stack

  12. 12

    PHPStorm missing return statement

  13. 13

    Missing return statement in for loop

  14. 14

    AsyncTask missing return statement

  15. 15

    Missing return UITableViewCell

  16. 16

    Missing return statement with switch

  17. 17

    Two Missing Return Statements

  18. 18

    Missing return statement in PhpStorm

  19. 19

    Return array missing values

  20. 20

    PHPStorm missing return statement

  21. 21

    Missing return statement

  22. 22

    Missing return statement in for loop

  23. 23

    Missing return statement with for loop

  24. 24

    AsyncTask missing return statement

  25. 25

    Missing a return statement somewhere?

  26. 26

    Missing return statements, loops

  27. 27

    Missing return in Swift

  28. 28

    I have a function returning a vector of strings. It takes two strings and is to return the strings in one that are missing in the other

  29. 29

    UICollectionReusableView for section header not working

HotTag

Archive