An Objective-C Example On Optimization

Multiple Choice

Out of the following three code snippets, only one is optimized. Which snippet is it?

Snippet 1

for (object in objects) {

  if ( [object tag] == kTagObjectOne ) {
    [object foo];
  }
  [object bar];
}

Snippet 2

for (object in objects) {

  if ( [object tag] == kTagObjectOne ) {

    [object foo];
    [object bar];

  } else {

    [object bar];

  }
}

Snippet 3

for (object in objects) {

 if ([object tag] != kTagObjectOne )
   [object bar];

}

object = [objects getChildByTag:kTagObjectOne];

if (object) {

  [object foo];
  [object bar];

}

Read full article here >>

Game AI – iPhone

Here is a little preview of an Cocos2d CCSprite that rotates to follow a player.

The Cocos2d iphone framework is an open source game programming framework written in Objective-C. The Cocos2d game programming framework targets iOS devices and OSX Mac platform.

Read full article here >>

CGPoint Pass By Reference

The following example shows how to pass CGPoint by reference in Objective-C.

Class Interface

@interface
  -(void)multiplyX:(CGPoint *)point by:(float)a
@end

Class Implementation

@implementation
-(void)multiplyX:(CGPoint *)point by:(float)a {
   (*point).x = (*point).x * a;
}
@end

Read full article here >>

CGRect, CGSize, CGPoint

If you have used any C structs in Objective-C programming for screen alignment or sizing then you have used either CGPoint, CGRect and CGSize.

The Objective-C programming language can appear a little intimidating due to it’s unique syntax. However, once your gain the syntax knowledge it is much less intimidating.

Read full article here >>