Value of variable not staying outside of method

Discussion in 'iOS Programming' started by MythicFrost, Oct 5, 2010.

  1. MythicFrost macrumors 68040

    MythicFrost

    Joined:
    Mar 11, 2009
    Location:
    Australia
    #1
    Hi,

    I have a variable called rootPage declared in projectViewController.h and its property has the attributes nonatomic and retain.
    Code:
    -(void)createItem:(SCItem *)parent:(NSString *)name {
        SCItem *item = [[SCItem alloc] init];
        item.name = name;
        if (parent == nil) {
            parent = item; 
        } else {
            [parent.children addObject:item]; //ignore this
        }
    }
    
    Code:
    -(void)loadItems {
        [self createItem:rootPage:@"Bookmarks"];
        SCItem *temp = rootPage;
        int k = 0;
    }
    When the first line of loadItem runs, it passes rootPage (uninstantiated) as the first parameter (parent), the method checks if its nil, and in this case, sets parent to the previously instantiated SCItem item.

    The problem is, when I place a break point on the line "int k = 0;", temp and rootPage both show as 0x0 (nil). However, I've placed a break point inside of the createItem method after parent has been set to item, and both show a pointer (data).

    How do I make the changes made to parent inside of the createItem method change rootPage and not just some local variable copy inside of the method.

    In my primary language (Visual Basic) you'd place ByRef in the parameter instead of ByVal, is there any similar thing here?
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    Before we even go into the various issues with the code you supplied, I would like to ask: what have you already done to educate yourself in the fundamentals of Objective-C / iPhone programming? Books? Videos? Tutorials? Etc? Be as specific as you can when you answer. Thank you.
     
  3. grimjim macrumors member

    Joined:
    May 24, 2003
    #3
    You seem to have a method:
    Code:
    -(void)createItem:(SCItem *)parent:(NSString *)name;
    Which you are calling using the following line:
    Code:
    [self createItem:rootPage:@"Bookmarks"];
    I would fix this for starters. :)
     
  4. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    Although that syntax is quite rare it is, strangely, valid: the name parameter is just, ironically, unnamed. The method definition could just as easily be written:
    Code:
    -(void)createItem:(SCItem *)parent :(NSString *)name;
     
  5. (marc) macrumors 6502a

    (marc)

    Joined:
    Sep 15, 2010
    Location:
    the woods
    #5
    You should get a book on Objective-C. That's the best advice you'll get.
     
  6. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #6
    Maybe they already have one. That's why I was asking before. No need to be demanding until we get an answer to that.
     
  7. ianray macrumors 6502

    Joined:
    Jun 22, 2010
    Location:
    @
    #7
    OP, are you familiar with pass-by-value and variable scopes in C? If so, the same rules apply to Objective-C.
     
  8. MythicFrost thread starter macrumors 68040

    MythicFrost

    Joined:
    Mar 11, 2009
    Location:
    Australia
    #8
    Thanks for all your replies,
    Practice, find a problem, look for the answer. I've read some documentation, watched a fair amount of good video tutorials, etc.
    I don't own any books, and as much as I'd appreciate suggestions, the most important thing right now is I figure out what to change here to get this working.
    Unfortunately that isn't the answer I'm looking for, I'm sure a book would be helpful, but right now, I've been unable to continue coding whilst this issue remains, so my time is being wasted, thus I'm quite eager to solve this so I can get on to the next problem.
    No, but I think I know what you mean by pass-by-value. My native language is Visual Basic -- back then C++ was too hard for me to understand so I started with VB.
    Ultimately, in VB, I would add "ByRef" at the start of the parameter instead of "ByVal", to get the desired result, I'm just wondering what the equivalent in Obj C is?

    I've posted on the Apple developer forums however the problem wasn't as refined now as it is here, my post wasn't as clear, so I didn't get any answers that solved the problem.
     
  9. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #9
    Stop. Learn the language and then come back to this problem. I know you consider it a waste of time, but it is nothing compared to the amount of time you are wasting on silly problems like the one you are having at the moment.

    Here is Apples free book on Objective-C. I suggest you read it. It is short and should get you to a stage where most of these problems will no longer bother you.

    http://developer.apple.com/library/...C.html#//apple_ref/doc/uid/TP30001163-CH1-SW2
     
  10. MythicFrost thread starter macrumors 68040

    MythicFrost

    Joined:
    Mar 11, 2009
    Location:
    Australia
    #10
    I don't consider it a waste of time, but I've got work to do and this is how I learn, I set a goal (an app to build), and I finish it; the problems I encounter along the way are how I learn, and the things I learn this way, I don't really forget.

    The problem I have with general reading, or, learning the language, is that it isn't what I need right now. The majority of the things I will have to read will be of little or no use to me for my little web browsing app I'm making.

    And, a lot of documentation which I read, I don't understand. I'm a very visual learner, I need to put it into practice, and this is the best way for me.
    I would appreciate someone telling me the solution to this, I need to get back to work.

    (PS: thanks for the link, I will read the document in the meantime)
     
  11. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #11
    Wow, I'm gobsmacked. You're not being paid to do this work, are you?
     
  12. MythicFrost thread starter macrumors 68040

    MythicFrost

    Joined:
    Mar 11, 2009
    Location:
    Australia
    #12
    Why? I came here for the answer. And no, I'm working for myself; I wouldn't accept a job from someone without knowing how to do it.

    I posted here because I needed help with a specific topic, it's apparent several of you know the answer, so why can't you just tell me?
    You might see it best that I go and read hours of documentation, but that isn't what I need now; it isn't the time.

    As I said, I learn visually and by doing -- when I run into problems, I get help, they get solved, I learn, I move to the next problem. If it doesn't get solved, I get stuck -- I've browsed some documentation several times looking for the specific answer I need, as well as searching the net, and posting on Apple's dev forum, the former two yielded no results, nor the latter, as when I posted, the problem wasn't as refined as it is now.
     
  13. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #13
    I could explain the problems with your code but without an understanding of the basic fundamentals of Objective-C, it would probably be meaningless to you. I could just give you the fixed code, but then I'm just doing your work for you, aren't I? So, forgive me if I elect to bow out of this discussion. Good luck, though! :)
     
  14. MythicFrost thread starter macrumors 68040

    MythicFrost

    Joined:
    Mar 11, 2009
    Location:
    Australia
    #14
    Not quite, in either scenario:

    1) I'd very likely understand it, and if not, it puts me further ahead in solving it than I was prior.
    2) Not quite, I've already written code. All you need to tell me is what to change, that shouldn't require much code to be written, if any at all.

    I've been told to replace the 'retain' attribute with the 'assign' attribute, as well as doing [item retain] in the method, amongst other things. Neither of which worked, but the solution should be, as implied by another poster, simple.

    EDIT: Anyway, thanks.

    EDIT 2:
    It's solved now.

    Code pretty much resembles this:
    Code:
    -(void)createItem:(SCItem **)parent:(NSString *)name {
        SCItem *item = [[SCItem alloc] init];
        item.isBookmark = isBookmark;
        item.name = name;
        if (name == @"Bookmarks") {
            *parent = item; 
        } else {
            //left this line out
        }
    }
    Code:
    -(void)loadItems {
        rootPage = [[SCItem alloc] init];
        [self createItem:&rootPage:@"Bookmarks"];
        int k = 0; //break point shows the changes made in createItem
    }
     
  15. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
  16. MythicFrost thread starter macrumors 68040

    MythicFrost

    Joined:
    Mar 11, 2009
    Location:
    Australia
    #16
    I know, I haven't released item. Thanks.

    I intend to ignore all my memory leaks for now, and then, when my app is finished, read up a bit on memory leaks, and then go on a cleaning spree.
     
  17. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #17
    Nope, it's not item that's leaking.
     
  18. MythicFrost thread starter macrumors 68040

    MythicFrost

    Joined:
    Mar 11, 2009
    Location:
    Australia
    #18
    Hmm, I'm going to assume it isn't parent then, and it can't be rootPage (I need it later).

    It must be name!
     
  19. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #19
    Again, nope.
     
  20. MythicFrost thread starter macrumors 68040

    MythicFrost

    Joined:
    Mar 11, 2009
    Location:
    Australia
    #20
    I don't think I have any variables left, not that I can see anyway, lol. I'll just have to read the documentation on memory leaks later and then go through my code, I'm sure there are plenty.

    Could it be comparing name to @"Bookmarks"? Oh, and I just realised, isBookmark is in my code, it shouldn't be in this case.
     
  21. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #21
    Actually, it's one of the ones you've already dismissed.
     
  22. MythicFrost thread starter macrumors 68040

    MythicFrost

    Joined:
    Mar 11, 2009
    Location:
    Australia
    #22
    Lol, rootPage, because its set to item, but is initialised prior. Haha, thanks. I'm going to be rofl if I'm wrong again and its parent lol.
     
  23. Luke Redpath macrumors 6502a

    Joined:
    Nov 9, 2007
    Location:
    Colchester, UK
    #23
    This really isn't the best way of going about things. You should learn the fundamentals of memory management first; the rules are quite simple and you'll save yourself a lot of headaches later.

    Another note about your code snippet; you should use the isEqualToString method of NSString to compare strings, not ==, which just compares the pointers (it's working by chance rather than design in your example due to the way that NSString literals work).
     
  24. MythicFrost thread starter macrumors 68040

    MythicFrost

    Joined:
    Mar 11, 2009
    Location:
    Australia
    #24
    1) I'll have a look at the memory management document, thanks
    2) Ah, yes, I've had to use that before; thanks.
     
  25. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #25
    Wirelessly posted (Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_2 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8C5101c Safari/6533.18.5)

    That's correct, sir!

    Also, if you're going to go to the trouble of creating a property, you should use the accessors for it. This will help handle some of the memory management as well.
     

Share This Page