Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Sep 7, 2010, 02:55 PM   #1
tooom
macrumors newbie
 
Join Date: Sep 2010
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   0 Reply With Quote
Old Sep 7, 2010, 03:09 PM   #2
Catfish_Man
macrumors 68030
 
Catfish_Man's Avatar
 
Join Date: Sep 2001
Location: Portland, OR
Send a message via AIM to Catfish_Man
You're doing [MyClass initMyClass] instead of [[MyClass alloc] initMyClass]. You have to allocate space for an object before you can initialize it.

<edit>
Corollary: +initMyClass should be -initMyClass. An instance method, not a class method.
</edit>
Catfish_Man is offline   0 Reply With Quote
Old Sep 7, 2010, 03:22 PM   #3
chown33
macrumors 603
 
Join Date: Aug 2009
Quote:
Originally Posted by tooom View Post
I'm starting with objective c.
...
What's wrong here?
You're not telling us how you're starting with Objective-C.

Are you following an online tutorial?
Which one? Post its URL.

Are you reading a book?
Which one? Post its title and author.

Are you haphazardly trying code from various places on the internet?
Not a good idea. Almost any book or tutorial would be better.
chown33 is offline   0 Reply With Quote
Old Sep 7, 2010, 04:31 PM   #4
jared_kipe
macrumors 68030
 
jared_kipe's Avatar
 
Join Date: Dec 2003
Location: Seattle
Send a message via AIM to jared_kipe
I think some of the confusion is on the usage of + vs - methods. I remember when I first started, I thought a lot of things should be + that really shouldn't be.

As stated before
+ methods can only be called on the class itself.
In Cocoa these are almost always convenience methods that return fresh objects or singleton instances.

For example, your class could be rewritten like this:
Code:
+ (MyClass *) newMyClass {
     MyClass *newObject = [[MyClass alloc] init];
     return [newObject autorelease];
}
Another example would be if you were writing a class to provide some math functionality..

Code:
+ (double) sin: (double) x {
     // some sin() implementation
}
This method could be a class method because it doesn't require an instance to do its work. (It obviously could be a C function instead of a class method.)
jared_kipe is offline   0 Reply With Quote
Old Sep 7, 2010, 07:48 PM   #5
Sydde
macrumors 68000
 
Sydde's Avatar
 
Join Date: Aug 2009
Quote:
Originally Posted by jared_kipe View Post
For example, your class could be rewritten like this:
Code:
+ (MyClass *) newMyClass {
     MyClass *newObject = [[MyClass alloc] init];
     return [newObject autorelease];
}
Hey there, oops: should you be using autorelease in a method that has "new" in the name? I mean, just for consistency sake.
__________________
You got to be a spirit. You can't be no ghost.
Sydde is offline   0 Reply With Quote
Old Sep 8, 2010, 04:16 AM   #6
tooom
Thread Starter
macrumors newbie
 
Join Date: Sep 2010
Okay, thanks for the answers. I've modified my code in the following way
Code:
int main (int argc, const char * argv[]) {
    NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
	
	printf("1\n");
	MyClass* myClass = [[MyClass alloc] 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;
}
Now, it runs. But I'm wondering if the object myClass has been added to the autoreleasepool?
tooom is offline   0 Reply With Quote
Old Sep 8, 2010, 12:12 PM   #7
Catfish_Man
macrumors 68030
 
Catfish_Man's Avatar
 
Join Date: Sep 2001
Location: Portland, OR
Send a message via AIM to Catfish_Man
Yes. Calling -autorelease on an object adds it to the topmost autorelease pool.
Catfish_Man is offline   0 Reply With Quote
Old Sep 21, 2010, 05:08 PM   #8
jared_kipe
macrumors 68030
 
jared_kipe's Avatar
 
Join Date: Dec 2003
Location: Seattle
Send a message via AIM to jared_kipe
Quote:
Originally Posted by Sydde View Post
Hey there, oops: should you be using autorelease in a method that has "new" in the name? I mean, just for consistency sake.
Generally yes. Since the method itself doesn't need to retain ownership. The caller should retain any object that it creates, and it is almost always assumed that the incoming object is autoreleased.
jared_kipe is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Simple Question Apple TV [ Thanks ] icloud87 Apple TV and Home Theater 10 Mar 5, 2014 06:29 PM
Simple question?? davidra Apple TV and Home Theater 5 Jan 21, 2014 01:19 PM
Simple Firefox question boynigel Mac Basics and Help 2 Apr 8, 2013 09:48 PM
Please Help! Simple photo question? Jema moda iPad Apps 1 Feb 9, 2013 05:58 AM
Simple ETF question. Southernboyj iPhone 6 Aug 17, 2012 06:36 AM

Forum Jump

All times are GMT -5. The time now is 04:39 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC