What is the recommended way to break out of a block or stop an enumeration?

So, I just understand that a break is just a loop or a switch.

enter image description here

Here is my question: is there a recommended way to break out of the block? For example:

func getContentFrom(group: ALAssetsGroup, withAssetFilter: ALAssetsFilter) { group.enumerateAssetsUsingBlock { (result, index , stop) -> Void in //I want to get out when I find the value because result contains 800++ elements } } 

I am using return now, but I'm not sure if this is recommended. Are there any other ways? Thanks guys.

+7
loops ios break swift
source share
2 answers

If you want to stop the current iteration of an enum, just return .

But you say:

I want to exit when I find the value, because the result contains 800 ++ elements

So this means that you want to completely stop the listing when you find the one you need. In this case, set the boolean value that pointer points to. Or the best name for this third parameter would be stop , for example:

 func getContentFrom(group: ALAssetsGroup, withAssetFilter: ALAssetsFilter) { group.enumerateAssetsUsingBlock() { result, index, stop in let found: Bool = ... if found { //I want to get out when I find the value because result contains 800++ elements stop.memory = true } } } 
+7
source share

return great, the concept of a block is like a function, so the return is fine.

Hope this helps .. :)

+6
source share

All Articles