mercredi 17 février 2010

Iphone programming basics

Starting with Iphone development you have to get used to some conventions in Objective C.

First you need some output for debugging in the Debugger Console, this is done with NSLog:

( By the way, A string is preceeded by @ )

NSLog(@"read the name");//simple string output in Debugger window.
Where to find this window: here, when you are running.....: (the gdb black window button...)

debugger.jpg

Now you can see what your code is doing:

NSLog(@"look at this number %d", intNumber); //int
NSLog(@"touched loc %f %f", currentTouch.x , currentTouch.y ); //float

showing a string in NSLog:
done by using stringByAppendingString:
NSLog([@"see how the name of the player is changed in setNamePlayer :" stringByAppendingString:@"new name of player"]);

mind the function clammers: [ and ] ..... [ ... stringByAppendingString: ... ], this gives a String, because this comes from the class NSString.

simple numbers: (no pointers needed)
int, float, bool
int myInt = 9;
float myFloat = 9.123;
bool myBool = false;

Declaration: int (float, bool)
in the header:

@interface ScoreObject : NSObject {
int playingFieldNumber;
}
@property int playingFieldNumber;

in the source:
#import "ScoreObject.h"
@implementation ScoreObject
@synthesize playingFieldNumber;

Number Objects: (always pointers in Objective C)
making an Number Object (for instance needed populating NSArray or NSMutableArray
NSNumber* myNumberObject = [NSNumber numberWithInt: 99 ];
NSNumber* myNumberObject = [NSNumber numberWithFloat: 99.99 ];

getting the value back from an NSNumber Object:
int myIntValue = (int)[myNumberObject intValue];
float myFloatValue = (float)[myNumberObject floatValue];
bool myBoolValue = (bool)[myNumberObject boolValue];

using this in NSLog:
NSLog([@"look at this number %d", (int)[myNumberObject intValue]);

Declaration: objects like NSNumber, NSArray
in the header
@interface ScoreObject : NSObject {
NSMutableArray *fieldNameArray;
}
@property (nonatomic, retain) NSMutableArray *fieldNameArray;

in the source:
@implementation ScoreObject
@synthesize fieldNameArray;

Last thing about NSLog... how to get an object out?

NSLog( @"this is my object: %@", myNSNumber );

of course you do not get a int float bool, like this, but an object name.]]>