PDA

View Full Version : Newbie Objective-C question/problem




rien333
Feb 10, 2012, 05:02 PM
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
Feb 10, 2012, 05:07 PM
Your aren't allocating before

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

What happens if you do that? Or if you use new instead

rien333
Feb 10, 2012, 05:19 PM
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
Feb 10, 2012, 05:21 PM
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

rien333
Feb 10, 2012, 05:36 PM
Shortened version with a bunch of yet to make methods removed


#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. ;)

h00ligan
Feb 10, 2012, 05:39 PM
@implementation is missing an @end statement

rien333
Feb 10, 2012, 05:48 PM
Oh, sorry, that is a pasting mistake on my side. The actual code does have a @end statement.

h00ligan
Feb 10, 2012, 05:51 PM
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.

chown33
Feb 10, 2012, 08:32 PM
RWKoolstofAtoom *myObject = [[myObject init] alloc]; // This line gives me an error message

That's completely wrong. It should be:
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:
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:
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
Feb 10, 2012, 08:34 PM
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
Feb 10, 2012, 08:55 PM
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
Feb 11, 2012, 01:02 AM
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 :o

rien333
Feb 11, 2012, 02:54 AM
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
Feb 11, 2012, 04:00 AM
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
Feb 11, 2012, 04:13 AM
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
Feb 11, 2012, 05:16 AM
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
Feb 11, 2012, 07:45 AM
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!