PrepareForSegue mystery

前端 未结 3 1820
暖寄归人
暖寄归人 2021-01-28 13:47

I\'ve got a prepareForSegue method in two different VCs. One uses an if statement, while the other is intended to use a switch. The code is virtually i

3条回答
  •  闹比i
    闹比i (楼主)
    2021-01-28 14:30

    There are two separate problems here.

    You can declare a variable in C/Objective-C in a switch-statement (without the need for an additional { ... } scope), but not immediately following a label. To solve this problem it is sufficient to insert a semicolon after the label:

    switch (i) {
        case 0: ;
            int i;
            // ...
            break;
    
        default:
            break;
    }
    

    Only if you declare Objective-C objects and compile with ARC, then you have to introduce an additional scope:

    switch (i) {
        case 0: {
            NSObject *obj;
            // ...
            } break;
    
        default:
            break;
    }
    

    The reason is that the ARC compiler needs to know the precise lifetime of the object.

提交回复
热议问题