I'm starting with objective c. I wrote the following stuff but I do not understand what I'm doing wrong
MyClass.h
MyClass.m
And Prog1.m
There are some includes from XCode that are not necessary (i know). The program terminates and i do not understand why. I get the following error message on the console:
What's wrong here?
MyClass.h
Code:
#import <Cocoa/Cocoa.h>
@interface MyClass : NSObject {}
+(MyClass*) initMyClass;
@end
MyClass.m
Code:
#import "MyClass.h"
@implementation MyClass
+(MyClass*) initMyClass {
self = [super init];
return self;
}
@end
And Prog1.m
Code:
#import <Foundation/NSObject.h>
#import <Foundation/NSAutoreleasePool.h>
#import <Foundation/NSString.h>
#import <Foundation/NSArray.h>
#import <Foundation/NSValue.h>
#import <stdio.h>
#import "MyClass.h"
int main (int argc, const char * argv[]) {
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
printf("1\n");
MyClass* myClass = [MyClass initMyClass];
printf("2\n");
[myClass autorelease];
printf("3\n");
MyClass* myClass2 = myClass;
printf("4\n");
[myClass2 retain];
printf("5\n");
[myClass release];
printf("6\n");
[pool release];
return 0;
}
There are some includes from XCode that are not necessary (i know). The program terminates and i do not understand why. I get the following error message on the console:
Code:
Program loaded.
run
[Switching to process 20067]
Running
1
2010-09-07 21:10:49.667 Prog1[20067:a0f] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** +[MyClass<0x100001080> init]: cannot init a class object.'
*** Call stack at first throw:
(
0 CoreFoundation 0x00007fff83a27cc4 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x00007fff86ef70f3 objc_exception_throw + 45
2 CoreFoundation 0x00007fff83a80f59 +[NSObject(NSObject) init] + 137
3 Prog1 0x0000000100000e67 +[MyClass initMyClass] + 57
4 Prog1 0x0000000100000d83 main + 103
5 Prog1 0x0000000100000d14 start + 52
6 ??? 0x0000000000000001 0x0 + 1
)
terminate called after throwing an instance of 'NSException'
Program received signal: SIGABRT.
sharedlibrary apply-load-rules all
What's wrong here?