Archive for the ‘Objective C’ Category

28
Feb
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if(editingStyle == UITableViewCellEditingStyleDelete)
    {
        //remove the deleted object from your data source.

    {
}

24
Feb
NSNumberFormatter * f = [[NSNumberFormatter alloc] init];
[f setNumberStyle:NSNumberFormatterDecimalStyle];
NSNumber * myNumber = [f numberFromString:@"42"];
[f release];

If the string is not a valid number, then myNumber will be nil. If it is a valid number, then you now have NSNumber to figure out what kind of number it actually is.

24
Feb

Say, if you see this warning:

warning: receiver ‘myCoolClass’ is a forward class and corresponding @interface may not exist

you need to #import the file, but you can do that in your implementation file (.m), and use the@class declaration in your header file.

@class does not (usually) remove the need to #import files, it just moves the requirement down closer to where the information is useful.

For Example

If you say @class myCoolClass, the compiler knows that it may see something like:

myCoolClass *myObject;

It doesn’t have to worry about anything other than myCoolClass is a valid class, and it should reserve room for a pointer to it (really, just a pointer). Thus, in your header, @class suffices 90% of the time.

However, if you ever need to create or access myObject‘s members, you’ll need to let the compiler know what those methods are. At this point (presumably in your implementation file), you’ll need to#import "myCoolClass.h", to tell the compiler additional information beyond just “this is a class”.

24
Feb

What you want is a High resolution timer class (using NSDate):

Output:

Total time was: 0.002027 milliseconds
Total time was: 0.000002 seconds
Total time was: 0.000000 minutes

Main:

Timer *timer = [[Timer alloc] init];

[timer startTimer];
// Do some work
[timer stopTimer];

NSLog(@"Total time was: %lf milliseconds", [timer timeElapsedInMilliseconds]);
NSLog(@"Total time was: %lf seconds", [timer timeElapsedInSeconds]);
NSLog(@"Total time was: %lf minutes", [timer timeElapsedInMinutes]);
Edit: Added methods for -timeElapsedInMilliseconds and -timeElapsedInMinutes

Timer.h:

#import

@interface Timer : NSObject {
NSDate *start;
NSDate *end;
}

- (void) startTimer;
- (void) stopTimer;
- (double) timeElapsedInSeconds;
- (double) timeElapsedInMilliseconds;
- (double) timeElapsedInMinutes;

@end

Timer.m

#import "Timer.h"

@implementation Timer

- (id) init {
self = [super init];
if (self != nil) {
start = nil;
end = nil;
}
return self;
}

- (void) startTimer {
start = [NSDate date];
}

- (void) stopTimer {
end = [NSDate date];
}

- (double) timeElapsedInSeconds {
return [end timeIntervalSinceDate:start];
}

- (double) timeElapsedInMilliseconds {
return [self timeElapsedInSeconds] * 1000.0f;
}

- (double) timeElapsedInMinutes {
return [self timeElapsedInSeconds] / 60.0f;
}

@end

24
Feb

You’ll need to use

+[NSTimer scheduledTimerWithTimeInterval:invocation:repeats:]

By default, the selector used to fire a timer takes one parameter. If you need something other than that, you have to create an NSInvocation object, which the timer will use instead.

24
Feb
-(void) myVoidMethod
{
    BOOL res;
    res = test();
}

24
Feb
YourAppDelegate *delegate =[[UIApplication sharedApplication] delegate];
[[delegate window] setBackgroundColor:[UIColor redColor]];

24
Feb
NSArray *stringArray = [NSArray arrayWithObjects:@"1", @"2", nil];
NSLog(@"count = %d", [stringArray count]);
(or)
NSString *array[2] = {@"1", @"2"}
//size of the memory needed for the array divided by the size of one element.
NSUInteger numElements = (NSUInteger) (sizeof(array) / sizeof(NSString*));

20
Feb

It is possible to convert GL point to UITouch point in cocos2D using the following code :

Consider a GL point, glLocation.

CGPoint location = [[CCDirector sharedDirector] convertToUI: glLocation];

// Now location will contain the UITouch point corresponding to GL point, glLocation.

, , ,

31
Jan
[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"]