"Null is returned from a method that is expected to return a non-zero value" with cellForRowAtIndexPath

In iOS 10, this new warning is being thrown.

What is the appropriate way to return from cellForRowAtIndexPath in cases where the method cannot determine the appropriate cell to return (for example, an exception)?

(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(tableView == self.selectedContentTableView) {
        // make cell...

        return cell;
    }
    else if(tableView == self.recipientTableView) {
        // make cell...

        return cell;
    }
    else {
        NSLog(@"Some exception message for unexpected tableView");

        return nil; // ??? what now instead
    }
}
+4
source share
2 answers

If this situation is a programming error that should not occur, then the corresponding action is to terminate the program with an error so that a programming error is detected and can be fixed:

(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(tableView == self.selectedContentTableView) {
        // make cell...

        return cell;
    }
    else if(tableView == self.recipientTableView) {
        // make cell...

        return cell;
    }
    else {
        NSLog(@"Some exception message for unexpected tableView");
        abort();
    }
}

abort() __attribute__((noreturn)), .

+4

, 100% . ( , ..), nil . , .

Apple Objective-C , . UITableView , tableView:cellForRowAtIndexPath: nil - UIKit , . , .

0

Source: https://habr.com/ru/post/1653832/


All Articles