Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > Mac OS X Development Forums > Objective-C, Python, Ruby Development

Reply
 
LinkBack Thread Tools Display Modes
Old 09-07-2010, 02:23 PM   #1 (permalink)
Registered Member
 
Join Date: Sep 2010
Posts: 58
tooom is on a distinguished road
Default Simple Question

I'm starting with objective c. I wrote the following stuff but I do not understand what I'm doing wrong

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?
tooom is offline   Reply With Quote
Old 09-09-2010, 01:57 PM   #2 (permalink)
Registered Member
 
Join Date: Oct 2008
Posts: 37
paulperry is on a distinguished road
Default

Without compiling your code, it looks like the problem is in your initMyClass static method:

+(MyClass*) initMyClass {
self = [super init];
return self;
}

self is an instance variable, and since you have used the plus (+) sign to signify a static method, self is not available (Although, I would think the compiler would at least warn you about this, if not give an error, but maybe not). You could probably get this to work like this:

+(MyClass*) initMyClass {
MyClass* mc;
mc = [super init];
return mc;
}

Which is what the error message is describing:

reason: '*** +[MyClass<0x100001080> init]: cannot init a class object.'

However, I think there are further issues, in that you shouldn't release anything in the autorelease pool. I am not at a Mac to confirm either of these. Although, I believe that is what you are running into.
__________________
My Apps: Word Zapper / MyThoughts+ / MyThoughts / Nothing / Chicks Only
paulperry is offline   Reply With Quote
Old 09-11-2010, 06:33 PM   #3 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

Your issue is that only an *instance* of a class has an init method. The class itself does not. A class is like a template for creating objects; you might have a class called "MyClass" and then several instances of that class.

This is not valid:
Code:
MyClass* myClass = [MyClass init];
Your initMyClass method doesn't help because it's a class method too. You can't call [self init] or [super init] inside that method because self and super still refer to the class, not an instance.

The usually way to get an instance from class is to call "alloc" on the class. This is more what you want:

Code:
// reserve some memory for an instance
MyClass *instance = [MyClass alloc];
// initialize that instance - we assign because there are special cases
// where we get a different address back
instance = [instance init];
That's usually written in shorthand like this:
Code:
MyClass *instance = [[MyClass alloc] init];
If you did want to write a class method that returns an object, it should alloc and init an object, autorelease it, and then return it.

EXTRA CREDIT: Classes are also objects in objective-C, but I don't want to confuse the issue.
__________________

Free Games!
smasher is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 478
15 members and 463 guests
7twenty7, AlanFloyd, AppsBlogger, David-T, iAppDeveloper, imac74, Jaxen66, lovoyl, Music Man, mutantskin, Sami Gh, SLIC, solardrift, unicornleo, usernametaken
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,683
Threads: 94,131
Posts: 402,932
Top Poster: BrianSlick (7,990)
Welcome to our newest member, unicornleo
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 10:11 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0