2

Does something like Python generator exist in objective-c ?

I have following code on few places, so is there some way to simplify it ?

int maxWinInRow = [self maxWinInRow];

// how many wins in row
for (int i=1; i <= maxWinInRow; i++) {

    NSString * key = [NSString stringWithFormat:@"%d",i];
    NSNumber * value = [_winsInRow valueForKey:key ];
    int numbeOfWinInRow = value.intValue;


    // only this line is specific
    gameScore = gameScore + ( pow(6,i) * numbeOfWinInRow);
}
1
  • 2
    Refactor it into a method you can call... Commented Apr 3, 2014 at 12:11

1 Answer 1

2

To be specific, there is no such generator pattern built inside Objective C programming language. However with the introduction of "blocks" in Objective C (and C with LLVM) it has become somewhat possible to build your own generator pattern in Objective C.

If you are serious to learn this, you can go through this article by Mike Ash.

However, please be cautious when working with blocks, as it occasionally incurs extra overhead of memory management or else it might result a potential memory leak in your app. So if you want to use the described pattern, please make sure you have the proper understanding of "blocks". Otherwise, I would always advise you to refactor your code to build a basic method that would work as your stub as described by Wain. Certainly that wont allow the lazy initialization that the pattern implies. However, it's a better than nothing solution in Objective C.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.