Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

continue is only allowed inside a loop

Tags:

swift

Is this a bug in Swift where any code placed inside an autoreleasepool thinks that it's not inside the loop? Is there a workaround for this rather then making a messy code by separating my code into multiple autorelease pools?

for (key, value) in dictionary {
    autoreleasepool {
        // Lots of allocation and lots of logic

        continue // Need to continue to the next loop

        // Lots of allocation and lots of logic
    }
}
like image 965
aryaxt Avatar asked Dec 20 '14 21:12

aryaxt


1 Answers

The argument of autoreleasepool is a closure, so you can just early return from the closure:

for (key, value) in dictionary {
    autoreleasepool {
        // Lots of allocation and lots of logic

        if someCondition { return } // Need to continue to the next loop

        // Lots of allocation and lots of logic
    }
}
like image 51
Martin R Avatar answered Oct 02 '22 10:10

Martin R