Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

rien333

macrumors regular
Original poster
Jun 29, 2010
167
0
The Netherlands
Hi,

I've encountered a little problem while doing some beginner programming in Objective-C. The problem is that I apparently don't understand how to use init. What I'm kind of (it is a little bigger, actually) trying to do is the following:
I want to have an ivar which holds the name of an object, declared as "NSString* name;" in the @interface section and then have it initialized with a string in the init method in my @implentation. The string basically has to be something like "No name given yet.". But when I call my init method in main, my string keeps being null/nil.

From my understanding, the problem lies in the following line of code:
AnIrrelevantClass* myObject = [myObject init];
With the problem being that the init method never gets called. (I included a NSLog call in my init method to check)

I suspect this has something to do with some new memory management techniques and with the fact that you normally chain an init with the alloc method, which doesn't work. Can someone please help me with this?

Thanks. :)
Also I apologize if some parts of my post are a bit vague/strange, but I'm quite tired right now. ;)
 

h00ligan

macrumors 68040
Apr 10, 2003
3,028
136
London
Your aren't allocating before

AnIrrelevantClass *myObject = [ [myObject alloc] init];

What happens if you do that? Or if you use new instead
 
Last edited:

rien333

macrumors regular
Original poster
Jun 29, 2010
167
0
The Netherlands
Hmm, unfortunately that didn't help. I get an error message saying:
"main.m: error: receiver type 'AnIrrelevantClass' for instance message does not declare a method with selector 'alloc' . "

Could it be me doing something wrong elsewhere in the program?
Also I think I'm using garbage collection, if that helps.
 

h00ligan

macrumors 68040
Apr 10, 2003
3,028
136
London
What's in your @interface section for AnIrrelevantClass? Can you Post the code? Also, the * should be in front of the object name not the end of the class.

Does the @interface sections start like this?

@interface IrrelevantClassName: NSObject
 
Last edited:

rien333

macrumors regular
Original poster
Jun 29, 2010
167
0
The Netherlands
Shortened version with a bunch of yet to make methods removed

Code:
#import <Foundation/Foundation.h>

@interface RWKoolstofAtoom : NSObject {
    
    NSString *name;
    
}

- (id)init;
- (NSString*) getName;


@end

#import "RWKoolstofAtomen.h"

@implementation RWKoolstofAtoom

- (id) init {
    
    if ( self = [super init] ) {
        NSLog(@"Init reached.");
        name = [name initWithString:@"Nothing added yet."];    //Gues that this could be a wrong way to assing a string to 'name'?
        
    }
    
    return(self);
} //init

- (NSString *)getName {
    return (name);         // Bad code? 
 }

@end

// main.m

#import <Foundation/Foundation.h>
#import "RWKoolstofAtomen.h"


int main (int argc, const char * argv[])
{

    @autoreleasepool {
        RWKoolstofAtoom *myObject = [[myObject init] alloc];     // This line gives me an error message
        NSLog(@"%@ ", [myObject getName]);
    }

    return 0;
}
If there is an better way to accomplish my needs btw, feel free to tell me. ;)
 
Last edited:

h00ligan

macrumors 68040
Apr 10, 2003
3,028
136
London
I'm still looking it over too.. i copied and pasted. I'm also new to this, so I'm just trying a few things!

I'm afraid I couldn't isolate it.. I feel it's in the if return statement. You're calling something that it isn't expecting... I just can't pin it down and I have to go!

Sorry :(

Please post the fix if you find it, hopefully someone more knowledgable than myself can come help you.
 
Last edited:

chown33

Moderator
Staff member
Aug 9, 2009
10,751
8,423
A sea of green
Code:
        RWKoolstofAtoom *myObject = [[myObject init] alloc];     // This line gives me an error message
That's completely wrong. It should be:
Code:
RWKoolstofAtoom *myObject = [[RWKoolstofAtoom alloc] init];
You should reread the part of the book or tutorial where it shows the correct usage of alloc and init. You aren't allowed to reverse them. You aren't allowed to say 'myObject' where it needs a class name. You aren't allowed to leave out alloc.

This is also wrong:
Code:
        name = [name initWithString:@"Nothing added yet."];    //Gues that this could be a wrong way to assing a string to 'name'?
Well, the comment is right: it's definitely the wrong way to assign a string to name. The right way is:
Code:
        name = @"Nothing added yet.";


If you're tired, you're making mistakes. Frankly, these are really simple things. If you don't understand them, you need to fix your brain before you can fix your code. The way to fix your brain is to rest, not program a bunch of bugs.
 

balamw

Moderator emeritus
Aug 16, 2005
19,366
979
New England
If you're tired, you're making mistakes. Frankly, these are really simple things. If you don't understand them, you need to fix your brain before you can fix your code. The way to fix your brain is to rest, not program a bunch of bugs.

This is exactly why Hillegass recommends loads of sleep when working through his books.

B
 

h00ligan

macrumors 68040
Apr 10, 2003
3,028
136
London
Nothing is simple when you first start. Is new, let's not crucify or be rude. Everyone has a skill set, knowledge sharing is great.
 

chrono1081

macrumors G3
Jan 26, 2008
8,456
4,164
Isla Nublar
If you're tired, you're making mistakes. Frankly, these are really simple things. If you don't understand them, you need to fix your brain before you can fix your code. The way to fix your brain is to rest, not program a bunch of bugs.

This!

This is exactly why Hillegass recommends loads of sleep when working through his books.
B

And this!

I am 100% guilty of programming when tired, just see any of my questions in this forum :eek:
 
Last edited by a moderator:

rien333

macrumors regular
Original poster
Jun 29, 2010
167
0
The Netherlands
Ok, thanks for the advise. :)
I already found it a little odd to do the initializing before the allocating.
And my code now works and stuff, so thanks. :)
 

chrono1081

macrumors G3
Jan 26, 2008
8,456
4,164
Isla Nublar
Ok, thanks for the advise. :)
I already found it a little odd to do the initializing before the allocating.
And my code now works and stuff, so thanks. :)

Well, think of it this way:

Say your program is a bookshelf filled with books (objects). You can't make a new book without room to put it on the bookshelf correct?

The same thing happens when you are doing an object initialization.

[myObject alloc] creates the space in memory to make the object (or space for a book on the bookshelf).
[myObject init] makes the object (or the book).

Because these two happen frequently, you have a nested statement like this:

[[myObject alloc] init] Which says in order 1. Create a spot in memory for my object 2. Create the object

Not the best analogy but I couldn't think of a better one ;)
 

rien333

macrumors regular
Original poster
Jun 29, 2010
167
0
The Netherlands
Well, I think your explanation was very clear, thanks! But that can be because I already understood the concept, because with my previous sentence "I already found it a little odd to do the initializing before the allocating. " I tried to say that I was pretty sure that you first had to allocate, but h00ligan advised me otherwise (maybe by accident?). ;)
 

chrono1081

macrumors G3
Jan 26, 2008
8,456
4,164
Isla Nublar
Well, I think your explanation was very clear, thanks! But that can be because I already understood the concept, because with my previous sentence "I already found it a little odd to do the initializing before the allocating. " I tried to say that I was pretty sure that you first had to allocate, but h00ligan advised me otherwise (maybe by accident?). ;)

Oh ok I see what you were saying. Sorry I misunderstood what you wrote :D
 

h00ligan

macrumors 68040
Apr 10, 2003
3,028
136
London
Yah I had typed it backwards but fixed it right after. I should have added an edit rather than just fixing it. You must have caught that in the few seconds before my edit. Sorry!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.