Posts Tagged ‘Cocoa’

A little word of warning on timers


There’s some really nice utility functions in Cocoa that can make life a lot easier. Take performSelector:withObject:afterDelay: for example. It will call a function for you after a specified delay. But beware! It is the amount of time experienced by the main thread, not real time. Say you say to cocoa to call you back in 8 hours, but you put your machine to sleep for 4 hours in that period. In this scenario your selector will be called in 8+4 hours.

The moral of this story? Only use performSelector:withObject:afterDelay: for short delays.

– Möller


Objective-C to Python converter


I’ve been doing a lot of PyObjC lately and I’m always annoyed at having to convert small snippets from Objective-C to Python. After some time I decided enough was enough so I spent some time coding it up finally. I present to you the Objective-C to Python converter! It will only convert the new style for loops and it has some other limitations, so you have to look over it to see that it’s correct, but it’s WAY smoother than doing it all manually. If you give it your entire .m file it will be pretty close to complete. Try this example code:

// create a new instance
Fraction *frac = [[Fraction alloc] init];
if (foo){ // set the values
[frac setNumerator: 1];  [frac setDenominator: 3];
else {
    // print it
    printf( "The fraction is: " );
    [frac print];
    printf( "\n" );
// free memory
[frac release];

return 0;

The source code is public domain.

– Möller