Heyo, recent switcher from Windows, and I've been trying my hand at ObjC to see if it's worth porting my C# (and heavy System.Console Library use) to ObjC + SDL.
I've basically been writing more and more complex "Hello, world" programs to get more and more familiar with dynamic typing/binding and some aspects of the Foundation.
so, this is my problem - I'm basically trying to get user input and have the user input define what the object the message is being sent to. Sorry for the ugly code - I was lazy since this was just a test app.
The problem is that I'm not getting output... [ Hello print ] is suppossed to output "Hello World" (and is a class method). I've tried printing and testing every single variable, and as far as I can tell, they're all valid and correct. "selector" has not changed from tests 1 and 2, where it correctly was used to send the message of "print." "helper" is non-nil, and the only possible class it can be set to is Hello (unless Apple documentation is wrong... but I also experimented in tests 1 and 2 with @"Hello " and it would give me run-time errors. I also tried doing [ helper print ] but i'm getting no output, so something is clearly wrong with helper... but it's non-nil!
Help!
EDIT - Hmm.. I just noticed that I was inputting "5" and entering Hello, which I expect would give me a segmentation fault of some kind since it has to be null terminated, right? So I tried mallocing to bytes + 1 (to account for the null byte), but the problem persists... I feel C# has something to do with this as it has corrupted my memory of C.
I've basically been writing more and more complex "Hello, world" programs to get more and more familiar with dynamic typing/binding and some aspects of the Foundation.
so, this is my problem - I'm basically trying to get user input and have the user input define what the object the message is being sent to. Sorry for the ugly code - I was lazy since this was just a test app.
Code:
#import <Foundation/Foundation.h>
#import "hello.h"
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
id helper = NULL;
SEL selector = NULL;
helper = NSClassFromString(@"Hello");
selector = NSSelectorFromString(@"print");
// tests 1 and 2 omitted because, you know, they work
// this part is for inputting the string without any whitespace
int bytes;
char *string;
scanf("%i", &bytes );
string = (char *) malloc( sizeof(char *) * bytes );
scanf("%s", string );
printf( "The string was entered with %i bytes.\n", bytes );
// test ot make sure it was inputted correctly
int i;
for ( i = 0 ; i < bytes ; i++ )
{
printf( "%c.\n", string[i] );
}
// test 3
NSString *nsstring = [[ NSString alloc ] initWithCString:string encoding:NSASCIIStringEncoding ];
helper = NSClassFromString( nsstring );
if ( helper = nil )
{
printf ( "Custom helper failed!\n" );
}
[ helper performSelector:selector ];
free( string );
[nsstring release];
[class release];
[pool release];
return 0;
}
The problem is that I'm not getting output... [ Hello print ] is suppossed to output "Hello World" (and is a class method). I've tried printing and testing every single variable, and as far as I can tell, they're all valid and correct. "selector" has not changed from tests 1 and 2, where it correctly was used to send the message of "print." "helper" is non-nil, and the only possible class it can be set to is Hello (unless Apple documentation is wrong... but I also experimented in tests 1 and 2 with @"Hello " and it would give me run-time errors. I also tried doing [ helper print ] but i'm getting no output, so something is clearly wrong with helper... but it's non-nil!
Help!
EDIT - Hmm.. I just noticed that I was inputting "5" and entering Hello, which I expect would give me a segmentation fault of some kind since it has to be null terminated, right? So I tried mallocing to bytes + 1 (to account for the null byte), but the problem persists... I feel C# has something to do with this as it has corrupted my memory of C.