Monthly Archives: January 2012

Incompatible Objective-C types initializing struct

This bizarre iPhone compiler error message had me tearing my hair out this morning. I’ve finally figured it out.

Here’s the scene: You have two classes, each class has an init method with the same name and taking the same parameters. Here’s an example:

@interface myClass1 : NSObject { }

-(myClass1 *)initWithName:(NSString *)name;

@end

@interface myClass2 : NSObject { }

-(myClass2 *)initWithName:(NSString *)name;

@end

Now try to initialize an instance of the first class:

myClass1 *c = [[myClass1 alloc] initWithName:@"Fred"];

You get Incompatible Objective-C types initializing ‘struct myClass2 *’, expected ‘struct myClass1 *’ which doesn’t make any sense because the initializer never even mentions myClass2.

The thing is that the program will run fine, it just gives this warning at compile-time because the compiler can’t tell the difference between two different methods with the same signature. I like my code to compile with no errors and no warnings because I’m very fussy. So even though this warning could be safely ignored, it still bugs me and I have to get rid of it!

So to shut the compiler up, I need to cast the object so it explicitly knows which method to call.

myClass1 *c = [(myClass1 *)[myClass1 alloc] initWithName:@"Fred"];

Casting always looks ugly to me but that’s the way around it.

NMEA Checksum Calculator

I’ve been doing a lot of GPS work lately. Here’s a little utility I wrote which I’d like to share with you all. It’s an NMEA checksum calculator written in PHP.

Try it out here: http://siliconsparrow.com/demos/nmeachecksum.php

Here’s the source code:


NMEA Checksum Calculator

// Simple NMEA Checksum calculator by Adam Pierce
// Created 12-Jan-2012.
// This code is public domain. Copy & share it all you like for any purpose.

if(array_key_exists('nmea',$_REQUEST))
{
$nmea = $_REQUEST['nmea'];
print "

Calculating checksum for: ".htmlspecialchars($nmea)."

rn";

$checksum = 0;
for($i = 0; $i < strlen($nmea); $i++)
{
$c = substr($nmea,$i,1);
$n = ord($c);
if($c == '$')
$checksum = 0;
else if($c == '*')
break;
else
$checksum ^= $n;
}
print "

Checksum is ".dechex($checksum)."

rn";
}
?>

Enter an NMEA String: