3

I have an NSMutableOrderedSet.

I need to enumerate it, and it looks like the only options built onto the set are block based. So picking the simplest of the block based options, I have something like this...

[anNSMutableOrderedSet enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
    if ([(SomeClass*)obj isWhatIWant]) {
        *stop = YES;
        // Ok, found what I'm looking for, but how do I get it out to the rest of the code?        
    }
}]

4 Answers 4

4

You can use __block to assign some value inside completion block.

__block yourClass *yourVariable;
[anNSMutableOrderedSet enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
    if ([(SomeClass*)obj isWhatYouWant]) {
        yourVariable = obj;
        *stop = YES; 
    }
}]

NSLog(@"Your variable value : %@",yourVariable);
3

You will need to pass in a call back/block of code to call out to.

- (void)someMethod
{
    [self enumerateWithCompletion:^(NSObject *aObject) {
        // Do something with result
    }];       
}

- (void)enumerateWithCompletion:(void (^)(NSObject *aObject))completion
{

[anNSMutableOrderedSet enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
    if ([(SomeClass*)obj isWhatIWant]) {
        *stop = YES;
        if (completion) {
            completion(obj);
        }
    }
}];
}

You could also use delegation, and call back to a delegate you have defined to return the object.

[self.delegate enumerationResultObject:obj];

UPDATE:

Realised enumerateObjectsUsingBlock: is actually called synchronously, so the better approach would be to use a __block variable. Callback would still work but could be construed as misleading.

7
  • Would it also be possible to use __block SomeClass *someClassVar outside the block, and then inside the block, do someClassVar = obj to assign the resulting object to the block variable located outside the block? I haven't tried it yet, but if I'm understanding the __block keyword correctly, it seems that would work Commented Mar 30, 2016 at 11:19
  • @Logicsaurus Rex yes this will work. for more info about __block keyword check this link : stackoverflow.com/questions/7080927/…
    – Raj Tandel
    Commented Mar 30, 2016 at 11:34
  • Yeah a block variable would also work. But be aware of scope when using this approach. I find using an explicit callback more readable but it is up to you and how it applies to your implementation. Good luck.
    – Tim
    Commented Mar 30, 2016 at 13:33
  • @Jeff Did you mean to enclose the enumeration code chunk inside the callback method. Won't that cause an infinite loop? Commented Mar 30, 2016 at 17:52
  • I didn't define a callback, I'll edit my answer for clarity with a callback.
    – Tim
    Commented Mar 30, 2016 at 20:28
1

In this case, the easiest thing would be to not use enumerateObjectsUsingBlock:, and just use fast enumeration instead:

for (SomeClass *obj in anNSMutableOrderedSet) {
    if ([obj isWhatIWant]) {
        yourVariable = obj;
        break;
    }
}
-1

Try With Weak Self

    __weak SomeClass *weakSelf = self;
    [anNSMutableOrderedSet enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([(SomeClass*)obj isWhatIWant]) {
            weakSelf = (SomeClass*)obj;
            *stop = YES;
            // Ok, found what I'm looking for, but how do I get it out to the rest of the code?
        }
    }];

//you Have to use weakSelf outside the block
1
  • This makes no sense and is a terrible idea. You are mixing up the type of self and the type of the objects in anNSMutableOrderedSet. In most cases those types will not be the same.
    – Kurt Revis
    Commented Apr 5, 2016 at 4:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.