Category

Coming from an object oriented programming such as actionscript or java this feature may seem a little different. At least it felt different to me. It took sometime for me to get the understanding. It was un-believable for me, at first, and all magical.
In ObjectiveC there is a feature called Category. Its another way to extend a class. If we compare with inheritance, well it can not be compared to that. As one might be knowing that, ObjectiveC itself, provides option for inheritance.
Category adds functionality to the class itself.
What?!! Yes, its that powerful. I must say it again. It adds functionality to the class itself. Its not inheritance. As with inheritance one must use the child class, to get the added functionality. But with “Category” the added functionality is available to the class itself. Wherever one might have used the class, if a new Category is added, that class just gets the functionality. Thats magic!! Yes, kind of. But then its fun and powerful, when you know the real power of it. “I can do that by adding functionality to my class file itself”, there you said it. Lets talk about situations.

Just imagine, you have a class and used it everywhere, and later you wanted to add some functionality to the class itself without ever touching the class file. The situation may arise, that one is using an external library of some kind. That library is maintained by someone else. So, you never worry about it and just use it. What if, you want to add some functionality to some class of that library. Ohh well, “I will change the class”. What if the licensing policy do not allow you to do it? Or better, lets say, if you change it and the library is updated! You have to change the updated class again! And chances are that, the functionality is now written with some other logic in the library, which makes your implementation of the class unusable. You have to re-write it in the class. And thats a lot of work, if you think the library is getting updated regularly.

Lets take a situation, where one subclasses a class and used it. Well, the same thing may happen, that the root lib is updated and used a different approach this time. All the subclass logic need to be updated!

In another scenario, one may need some more functionality to the class. Well, we subclass it again. Nice and fine as long as you have used it in some places you remember. There may be cases one need to remove the first subclass and add the subclass of the first subclass. And thats a lot of work too.

Lastly, what if one needs to add functionality to the base SDK or the parent library. Subclass it and use it, simple. But we have already seen problems with subclassing. One has already used the base class through out the project, but later on it is required to add some functionality to the class. Either some how one has to add the functionality to the base class or subclass it and use the subclass everywhere, the base class was used previously.

Ok, coming back to Category. It allows a user to add functionality to the base class itself. One need not touch the class itself. It can be done even to the original ObjectiveC classes shipped with Apple SDK. That means, if we have used a class and later decide we need to add some more functionality to it, we are safe on with Category. As soon as it is declared, the functionality is available to all the places, where the base class is used.

Lets see how we can do it, some syntaxes and naming conventions.

Defining a Category, is kind of same a defining an ObjectiveC class. One needs 2 files for it. An interface file and an implementation file. The file naming conventions are generally as “ClassName+CategoryName.h” and “ClassName+CategoryName.m”. Where “ClassName” is the name of the class one needs to extend or add functionality to and “CategoryName” is the name of the Category one is defining now.
Each Category in a project must have a different name.
Categories can not define or add instance variables to a class.
Categories can override methods of the parent class.

Lets define a category :

//file name GameAddition+ShadowText.h
#import 
@interface GameAddition (ShadowText) 
-(SPSprite *)makeText:(NSString *)text 
			withColor:(int)colorValue 
			  andSize:(int)size 
	  withShadowColor:(int)shadowColor;
@end

And the implementation file will look as

//file name GameAddition+ShadowText.m
#import "GameAddition+ShadowText.h"
@implementation GameAddition(ShadowText)
-(SPSprite *)makeText:(NSString *)text 
                    withColor:(int)colorValue 
                       andSize:(int)size 
        withShadowColor:(int)shadowColor
{
	//custom code goes here
}
@end

the difference here is both in interface and implementation file the declaration is changed to

@interface ClassName (CategoryName)

instead of just

@interface ClassName 

And same happens with implementation file too, instead of

@implementation ClassName 

the category implementation file will declare it as

@implementation ClassName (CategoryName) 

. The category name will be on parenthesis.

Now thats all to it. Wherever we would have used the class “GameAddition”, a new method is available to it. But then we need some work there too. Just to make the application know we are using the category, wherever we have imported “GameAddition.h”, we have to import “GameAddition+ShadowText.h” instead. And there we go.

Hope that helps someone out there.

References :
1. http://cocoadevcentral.com
2. http://macdevelopertips.com

Tested my first iphone game on device

MathIsFun2
I feel so happy to get my opensource game tested on some real devices. The game got tested with iphone3GS, iphone4 and ipodtouch2G. Thanks to all my friends to help me with this. You all rock. And with that I come to a point where may be this game is pushed to Appstore. The tests were good and made some changes as per the retina display of new iPhone4.
Overall it is nice experience to see my game on device.
Cheers to life.

My opensource iphone game now supports accelerometer

I have made some improvements since my last post about the game. The game (Math Is Fun 2) now supports accelerometer. The new version of the game is having below mentioned additions and updations to it.

1. Accelerometer support ( The game mode is now changed by changing phone’s direction )
2. A counter is added ( Which changes question in every 1000 count )
3. Game now displays, total questions played ( asked )
4. Total number of correct answers given by user is displayed
5. Total number of wrong answers given by user is displayed

The code is on GitHub and licensed under Unlicense.

javascript library for converting SWFs to HTML/CSS3

When HTML and CSS3 are really catching up, its the animations which need a way to go to the web without flash plugin. Fortunately we have some very promising javascript libraries out there which does this. The two must mention libraries are
Gordon : http://github.com/tobeytailor/gordon/wiki
Smoke Screen : http://smokescreen.us/

While Adobe showed a tool on this years MAX, 2 days back, which converts FLA animations to HTML5/CSS3, its time to wait and watch, when people will have access to the tool.

My first iphone game goes opensource

I have recently finished an iphone game. This is written in Sparrow framework. I have decided to make it opensource and so put it up on github at this link. The game is very simple and is licensed under Unlicense. So there is no restriction as to how to use the code. You can use it to make money or what so ever as this licensing does not force you to do anything you do not need to do.
About the game, its a simple game of Mathematics for small kids. There are options for addition, subtraction, multiplication and division. Choose your type to play and go ahead playing it. There is no score, no time limit. The responses are immediate. So one will know whether correct or incorrect answer.
While I am developing on it too. Will love to see what others can do with it. This can be seen as code example for the Sparrow framework as well.

Making a default icon and a splash screen for your iphone app?

This is a real surprise to me, when I was trying to search as to how to make an icon for my game on the iphone screen. Along with this came in the simplicity to create a splash screen for the same game. First thought there would be some procedure involved in it. Then it seemed the procedure is to name two files as per the naming convention (yes, its not configuration but convention, the principle of convention over configuration) and then rest is just taken care of.
So if we put 2 images inside our project resources folder and name them as “Icon.png” and “Default.png” we are done with the making of our icon and splash screen.
Well, the sizes also take some importance, in my case “Icon.png” is of “57×57″ and “Default.png” is of “320×480″ px. If after naming appropriately the icon and the splash screen do not show, up make sure that their dimensions match exactly as mentioned now.

Hope that helps someone out there.

NSLog options

I always have a problem understanding the log syntax of objectiveC. So putting down it here. I have taken this from the original link, which is at CocoaDev website.

%@     Object
%d, %i signed int
%u     unsigned int
%f     float/double

%x, %X hexadecimal int
%o     octal int
%zu    size_t
%p     pointer
%e     float/double (in scientific notation)
%g     float/double (as %f or %e, depending on value)
%s     C string (bytes)
%S     C string (unichar)
%.*s   Pascal string (requires two arguments, 
           pass pstr[0] as the first, pstr+1 as the second)
%c     character
%C     unichar

%lld   long long
%llu   unsigned long long
%Lf    long double

Happy Logging