Silly property question

Discussion in 'Mac Programming' started by ace2600, Jun 12, 2008.

  1. macrumors member

    Joined:
    Mar 16, 2008
    Location:
    Austin, Texas
    #1
    Hi,

    Given something like
    PHP:
    @interface ExampleClass NSObject {
        
    NSObject *prop;
    }

    @
    property (nonatomic,retainNSObject *prop;
    If I access prop within ExampleClass am I accessing it as a property (using the property getters and setters) or am I accessing the variable directly? If directly, do I need to call self.prop to access using the property getters/setters/etc.?

    Also, does anyone know of links to good examples on retain vs assign. I've read a few articles, but I feel example situations on when you'd use one over the other would be helpful.
     
  2. macrumors regular

    Joined:
    Mar 27, 2008
    #2
    In my book it says that in Obj-C 2.0 garbage collection, retain and assign have no real difference because the GC will pick em up automatically. If I recall though.

    The book also says that if you are using the property to instantiate auto getters/setters then you would need to use "dot" notation, for eaxample use self.property over [property doesSomething].
     
  3. macrumors 6502

    Joined:
    May 12, 2003
    #3
    Yes, access to prop would be direct. self.prop would use the getter/setter methods.

    I don't know of any resources, no. But if you understand what retain means, and where you should and shouldn't use it, it's not too hard. If you don't know how/when to use retain, definitely learn it ASAP. It's pretty fundamental.

    I would say as a rule of thumb, you're going to want to retain anything that inherits from NSObject. You'd use assign for primitives, like int, float, BOOL, and also for things like structs.
     
  4. macrumors regular

    Joined:
    Mar 27, 2008
    #4
    *SMACKHEAD* Duh. I forgot about that till I read your post.
     
  5. thread starter macrumors member

    Joined:
    Mar 16, 2008
    Location:
    Austin, Texas
    #5
    Thanks for the help!

    self.prop = nil has the same affect as [prop release]? If so, is there a preference on the best way to deallocate the variable?
     
  6. macrumors 68030

    Catfish_Man

    Joined:
    Sep 13, 2001
    Location:
    Portland, OR
    #6
    Interesting question :) I would try it and see. Something like

    Code:
    NSLog(@"Retain count is: %d", [self.prop retainCount]);
    self.prop = nil;
    NSLog(@"Retain count is: %d", [self.prop retainCount]);
    
     

Share This Page