Search code examples
iosswiftscopescoping

How to make a subscope in Swift?


In C I can insert pairs of brackets to indicate a subscope.

int main() {
    int x = 5;
    {
        int y = 6;
        x += y; //Works
    }
    x = y+8; //Uh oh
}

In Swift, if I try something similar it will assume I'm trying to call the previous statement as a function, or errors for other reasons. How do I simply make a subscope in Swift, to deallocate variables earlier?


Solution

  • You can try

    do {   
         // insert sub scope code
    }