Noob is puzzled

Discussion in 'iOS Programming' started by .\\axxx, Oct 16, 2011.

  1. .\\axxx macrumors newbie

    Joined:
    Oct 16, 2011
    Location:
    Brisbane, Australia
    #1
    Hi,
    I'm an experienced developer but very new to the Mac and Objective C

    I'm teaching myself by developing a game. One thing has be confused. (To be honest, about a thousand things have me confused, but this one is just puzzling the poop out of me!)

    I have a class with a property defined as

    Code:
    @property (nonatomic, retain) NSMutableArray* terrain;
    and in the class I have

    Code:
    @synthesize terrain;
    Then, in a method I have

    Code:
    -(void)setUpLayer
    {
        self.terrain = foo;
    }
    All good, works fine.

    Originally, I had the following code:
    Code:
    -(void)setUpLayer
    {
        terrain = foo;
    }
    which built fine, and ran but crashed on that line.

    I guess that if you have to specify self. then that's just the way things are (I come from a C# background mainly, where the equivalent is to use this.terrain or just terrain pretty much interchangeably) but I don't understand why it would compile?

    What does 'terrain = foo;' mean, if not self.terrain = foo;?
     
  2. Sydde macrumors 68020

    Sydde

    Joined:
    Aug 17, 2009
    #2
    When you use self.terrain, you are actually making a method call which, in this case, would look something like
    Code:
    - (void)setTerrain:(NSMutableArray *)array {
       if ( terrain != nil ) [terrain release];
       terrain = [array retain];
    }
    This method, along with the complementary getter method, is created by the compiler when you invoke @synthesize — unless you actually write one or both of these methods yourself.

    When you use terrain without the dot-syntax, you are directly accessing the ivar itself (which could be important if you are using KVO). I suspect you may be having some kind of ARC-related issue.
     
  3. xStep macrumors 68000

    Joined:
    Jan 28, 2003
    Location:
    Less lost in L.A.
    #3
    The first copies the pointer value from foo to terrain. The crash likely comes from the fact that you are not placing a retain on terrain and foo either isn't retained or has been released by the time you arrive at the crash location. So at the crash location you are referencing an invalid pointer. Sometimes you can get lucky and it will work, but that isn't guaranteed without at least 1 retain count against it.

    The second copies the pointer but also performs a retain as Sydde pointed out. So when you get to the crash location, the crash does not occur because you are referencing a valid object.

    A side note. Don't do the following when using @synthesize'd ivars and your own custom set method.

    Code:
    - (void)setTerrain:(NSMutableArray *)array {
       if ( terrain != nil ) [terrain release];
       [COLOR="Red"]self.terrain[/COLOR] = [array retain];
    }
    
    That would put you into an endless loop! Why? Because that particular assignment calls the set method.
     
  4. .\\axxx thread starter macrumors newbie

    Joined:
    Oct 16, 2011
    Location:
    Brisbane, Australia
    #4
    Hi, and thanks for teking the time to help out.
    Well, I don't know what KVO is, but if it's accessing the iVar itself, I don't see why it wouldn't work identically? Isn't the net result of using the setter the same as setting the iVar directly (I haven't written my own setter, just synthesised)

    It's not an ARC issue as I'm not using ARC.
     
  5. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #5
    It's not the same. Because of the retain property attribute, it does more than just set the ivar. It release the old value and the retains the new value. A straight assign of an ivar will do neither.
     
  6. .\\axxx thread starter macrumors newbie

    Joined:
    Oct 16, 2011
    Location:
    Brisbane, Australia
    #6
    Ahh! I think I see. Thanks. So, if I understand you, the retain in the property definition tells it to keep the object in memory because I have a reference to it (the iVar behind the terrain property). But if I set the terrain iVar directly, once the method I am in terminates, Objective C runtime might dispose of the object before something else gets around to using it?

    If this is the case, then is the practice to simply never set the iVar directly, but to always access it through a retained property? Also (sorry, question begets question...) why would you ever NOT retain on a property?

    ----------

    Yep - thanks! I'm beginning to get my head around the retain stuff, I think!

    So if I said

    terrain = [foo retain];

    I'd probably prevent the crash?

    *(I'm not suggesting that it's a good idea, just trying to be clear in my head what's happening)
     
  7. xStep macrumors 68000

    Joined:
    Jan 28, 2003
    Location:
    Less lost in L.A.
    #7
    Simple answer; Yes. As you demonstrated below, you can retain it if you need it outside the scope of the method you are currently in.


    I think a good rule of thumb is to use "self.ivar =" or "object.ivar =" for assignments when you have the synthesize setup. It takes care of copy or retain you've placed into your property definition. The exception as I described previous would not apply within the ivars custom set method, which requires extra work.

    You would not retain an ivar within a method if your only use for it was within the scope of that method. For instance, you may create a custom formatted non retained NSString to a local variable, and assign the end result to some NSString ivar. The assignment to the ivar would perform it's own retain.




    That is fine. You can also do this which I think would be more popular in this case;
    Code:
    terrain = foo;
    [terrain retain];
    
    Clearly you need to do some reading about Objective-C and Cocoa memory management. I'm not sure that is the best link to send you to, but it is a start. You'll also need to understand also what methods return retained objects. There are fairly set rules for this.
     
  8. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #8
    That is why I usually recommend for anyone to name the ivar that backs your property slightly differently. This can be done via something like:
    Code:
    @synthesize terrain = _terrain;
    That way, if you try:
    Code:
    terrain = foo;
    you'll get a compile-time error.
     
  9. .\\axxx thread starter macrumors newbie

    Joined:
    Oct 16, 2011
    Location:
    Brisbane, Australia
    #9
    Thanks. I love simple answers :)


    Thanks - I am doing LOTS of reading - but there's nothing like one-on-one help, for which I thank you!
    I personally find that I need to do in order to learn - and so learn by my mistakes (so I learn a LOT!!!)

    ----------

    Awesome!
    That will really help me. Firstly, it makes sense as in other languages I would use exactly that method (naming the backing-variable of a property _propertyName) and secondly, when I forget to access the property via the setter (which I will!) it won't compile!
     
  10. xStep macrumors 68000

    Joined:
    Jan 28, 2003
    Location:
    Less lost in L.A.
    #10
    Don't know where you are, but you might want to look to see if there is an NSCoders Night, CocoaHeads, or other iOS/OS X meeting in your area. They can be great for mingling with others who can help you and perhaps you can help someone else. I like the Santa Monica NSCoders for the camaraderie and openness of people to help and give opinions. We're a pretty loose group who don't nerd out on just coding subjects. :)

    You'll find many people on this forum very willing to help out. It is a very nice group.
     
  11. .\\axxx thread starter macrumors newbie

    Joined:
    Oct 16, 2011
    Location:
    Brisbane, Australia
    #11
    How rude of me - I updated my profile...
    so far that's just what I have found. I was pleasantly surprised, I have to say, by the lack of "RTFM" responses :)

    I will search for a Brisbane group - I'm sure there must be one - we have a Mac store after all!
     
  12. xStep macrumors 68000

    Joined:
    Jan 28, 2003
    Location:
    Less lost in L.A.
    #12
    Beautiful city. Been through it once, way back when.


    Sometimes you'll get "RTFM", but most people give you a fighting chance at first and will be more tactful.


    Do you mean the "Apple Store" in Chermside? Those are not referred to as Mac stores. Mac is just a product. ;)

    The two sites to start with, that I forgot to link to earlier, are Cocoaheads.org and NSCoderNight.com. They aren't as well updated as they should be, but it is a start. Sites like Meetup.com could be handy too.
     
  13. .\\axxx thread starter macrumors newbie

    Joined:
    Oct 16, 2011
    Location:
    Brisbane, Australia
    #13
    Next time, drop me a line & I will buy you a beer!

    It's all been good so far. I find one of the problems as an Objective-C noob is knowing what to ask, as the terminology is all so new (I had to look up 'iVar' after one of the replies above, for example!)

    Yep:eek:

    Thanks for them - no Brissie one on the NSCoder night, and the Cocoa site's last update for Brisbane was in 2009 :eek:

    I'm just checking out Meetup.com (never heard of it before) and seems like, even if I don't find a mac Dev group, I can be entertained by the local Vampire group:D
     
  14. xStep macrumors 68000

    Joined:
    Jan 28, 2003
    Location:
    Less lost in L.A.
    #14
    LOL! Well, entertainment is important.

    You could start your own group. The difficult part is finding a place to put up with you all. Currently our NSCoder Night group meet in the back room of a restaurant and we previously met at a cafe that had tables we could line up. I think we've had as many has 15 people show up.

    Thanks for the offer. It will likely be a long while until I get to experience Brisbane again. :(
     
  15. Shawnpk macrumors 6502

    Joined:
    Jan 13, 2011
    Location:
    Los Angeles, CA
    #15
    I live here in Southern California and I would love to be part of the Santa Monica NSCoders, but when I checked google, the last post I found was in 2009. Where can I get more info?
     
  16. xStep macrumors 68000

    Joined:
    Jan 28, 2003
    Location:
    Less lost in L.A.
    #16
    Yea, that is a little messed up. The correct information in on the Wiki side of the site.
     

Share This Page