Hi I'm finding a way to enforce runtime type checking or such things in Objective-C on Cocoa.
This is my code sample. I expected runtime error about wrong assignment to variable 'b'. But it wasn't. Compiled and executed without any error.
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSArray* a = [NSArray arrayWithObject: @"TEST"];
NSURL* b = [a objectAtIndex:0];
NSLog(@"Is this URL? %i", [b isKindOfClass:NSURL.class]);
[pool drain];
return 0;
}
// Console log after program execution:
// 2010-01-11 10:25:02.948 Type Checking[98473:a0f] Is this URL? 0
I surprised about there is no runtime type checking is there. Because I used all high level languages like VB, C#, Java, ActionScript... I don't know low-level language like C, so I can't sure this is right way... It was really hard to figuring out why there is no compile or runtime error. But I'm getting to understand this as a natural rule in real C world. But more strong type checking will help me a lot. Even only in debugging session. Is there any way to do this?
And if there is no runtime type checking, what kind of coding and debugging strategy do I have to use about wrong typed values? And what's the trade off between runtime type checking is or isn't?