Newbie Objective-C question/problem

Discussion in 'Mac Programming' started by rien333, Feb 10, 2012.

  1. macrumors regular

    Joined:
    Jun 29, 2010
    Location:
    The Netherlands
    #1
    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. ;)
     
  2. h00ligan, Feb 10, 2012
    Last edited: Feb 10, 2012

    macrumors 68030

    Joined:
    Apr 10, 2003
    Location:
    A hot desert
    #2
    Your aren't allocating before

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

    What happens if you do that? Or if you use new instead
     
  3. thread starter macrumors regular

    Joined:
    Jun 29, 2010
    Location:
    The Netherlands
    #3
    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.
     
  4. h00ligan, Feb 10, 2012
    Last edited: Feb 10, 2012

    macrumors 68030

    Joined:
    Apr 10, 2003
    Location:
    A hot desert
    #4
    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
     
  5. rien333, Feb 10, 2012
    Last edited: Feb 10, 2012

    thread starter macrumors regular

    Joined:
    Jun 29, 2010
    Location:
    The Netherlands
    #5
    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. ;)
     
  6. macrumors 68030

    Joined:
    Apr 10, 2003
    Location:
    A hot desert
  7. thread starter macrumors regular

    Joined:
    Jun 29, 2010
    Location:
    The Netherlands
    #7
    Oh, sorry, that is a pasting mistake on my side. The actual code does have a @end statement.
     
  8. h00ligan, Feb 10, 2012
    Last edited: Feb 10, 2012

    macrumors 68030

    Joined:
    Apr 10, 2003
    Location:
    A hot desert
    #8
    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.
     
  9. macrumors 603

    Joined:
    Aug 9, 2009
    #9
    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.
     
  10. Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #10
    This is exactly why Hillegass recommends loads of sleep when working through his books.

    B
     
  11. macrumors 68030

    Joined:
    Apr 10, 2003
    Location:
    A hot desert
    #11
    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.
     
  12. chrono1081, Feb 10, 2012
    Last edited by a moderator: Feb 13, 2012

    macrumors 604

    chrono1081

    Joined:
    Jan 26, 2008
    Location:
    Isla Nublar
    #12
    This!

    And this!

    I am 100% guilty of programming when tired, just see any of my questions in this forum :eek:
     
  13. thread starter macrumors regular

    Joined:
    Jun 29, 2010
    Location:
    The Netherlands
    #13
    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. :)
     
  14. macrumors 604

    chrono1081

    Joined:
    Jan 26, 2008
    Location:
    Isla Nublar
    #14
    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 ;)
     
  15. thread starter macrumors regular

    Joined:
    Jun 29, 2010
    Location:
    The Netherlands
    #15
    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?). ;)
     
  16. macrumors 604

    chrono1081

    Joined:
    Jan 26, 2008
    Location:
    Isla Nublar
    #16
    Oh ok I see what you were saying. Sorry I misunderstood what you wrote :D
     
  17. macrumors 68030

    Joined:
    Apr 10, 2003
    Location:
    A hot desert
    #17
    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!
     

Share This Page