Archive: Missing return in a function; Simulator is fine

I have the following code for my UIPickerViews:


func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int
    {
        if (pickerView == pckLanguage)
        {
            return 1;
        }
        else
        {
            assert(false);
        }
    }
  
    func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int
    {
        if (pickerView == pckLanguage)
        {
            return self.listOfLanguages.count
        }
        else
        {
            assert(false);
        }
    }


When I compile the application in Simulator or on my device, I have no problem. Tried to archive the application and it gave me errors:


Missing return in a function expected to return int.


Very weird. I did not have such problems in my other (Objective C) applications.


Thanks a lot in advance.

Replies

Check for missed cases - the compiler may sense at least one...

assert is not used in release builds, just development, that's why you get the warning. If you really want that in a release build then you need to use precondition instead.

No, we're not talking about asserts here. The discussion right here is about a warning that appears in archive builds (release) and not in the simulator (debug). Sounds to me like something the static analyzer found.

I think Gargoyle's point is that if the compiler leaves the asserts out for an archive build, you end up with functions that do not return from the (now empty) else clause.

EDIT: Technically it is because there is no return in the else clause or outside (after) the if statement.

Asserts do not return, and they are not marked @noreturn as are fatalError() and preconditionFailure(). Assert or not, there is nothing in the first code to stop execution.

Sorry, I wasn't precise enough. The functions will no longer return an int if the else clause executes. Hence the error message "Missing return in a function expected to return int."

EDIT: I don't know why the compiler doesn't give the same error for debug builds when the assert is there, I'm just agreeing with Gargoyle that when the asserts are not there in archive builds, and the error message certainly seems to fit.

The funniest thing is that a similar code worked for me till now in release versions. Not sure what changed, either the Xcode upgrade or moving to swift. Thank you all, guys!