Monday, July 19, 2010

iPhone Dev

It's been a while since I wrote anything, so I feel compelled to write a little after reading my friend's blog. I'm not as eloquent as other people, but its fun to try :)

So lately I've been working with the iPhone SDK pretty exclusively when it comes to at home programming. I've found a lot of interesting classes, and I've ever so slowly been re-creating my XNA font engine and refactoring all my code to be nice and neat. When I first started with objective-c it was a nightmare. Everything is so different and ugly, but slowly I'm getting used to it. I already found delegates and function pointer equivalents which is great cuz it really makes some patterns much easier to implement.

None the less, development goes slowly, I worked out a pretty functional level generating algorithm which is great because its a step closer to the end goal. I let some people try my game at dinner today but they all agreed "A game you can't lose at isn't all that fun". So I'm starting to reconsider the whole "Never die" mentality I was going to go with for this game. Perhaps some burning lava pits and sharks are in order... Everyone loves sharks... or pandas... hmm. Something to think about =)

In the mean time, lets talk a little bit about those Objective-C function pointers. They are called Blocks. In the example below I will do Strategy Pattern using a function pointer block. Blocks are sadly are extremely over complicated (as is everything in Objective-C), so to make life easier it's recommended to use typedefs:

typedef void(^BasicBlock)(id);
Breaking down that syntax:

typdef --ReturnType--(^--BlockName--)(--Params--);

Then where you want to use your method you do something like:

-(void) Iterate:(BasicBlock) theStratYouWishToUse;

Then in the definition we need to actually do something with that function like call it:

-(void) Iterate:(BasicBlock) theStratYouWishToUse 
NSEnumerator * enumerator = [ spriteList objectEnumerator ];
id obj;

while ( obj = [enumerator nextObject] )


So that takes care of defining it, and calling it, but how do we create the block to pass it into our object? Remember, we have to worry about garabage collection and all that good jazz, but Apple's SDK has some stuff for that =)

BasicBlock myBlock = [[^ (id obj)
[obj doSomethingAwesome];

} copy] autorelease];

The copy and autorelease are methods that apple added, onto the block, the block syntax is:
BlockType blockObjectName = [[^--paramList-- { stuff... } copy] autorelease];

After that, you just pass ur block around like it was an object:

[SpriteList Iterate: blockObjectName ];

Thats basically it, if you had a return type, you'd just return it at the end of ur method, and you're all set =)

Anyway, there's my tech for the day, hopefully that helps someone out there one day =)

No comments: