Cocoa 2.0 properties

Discussion in 'Mac Programming' started by nacho4d, Jul 17, 2008.

  1. nacho4d macrumors newbie

    Joined:
    Jul 13, 2008
    #1
    I am new to cocoa 2.0 (but not new to Cocoa).
    I am trying to do the iPhone tutorials... and sometimes i see @property ...
    Can anyone tell me the difference between a @property and a simple class attribute?
    Thanks in advance.
     
  2. masonmey macrumors newbie

    Joined:
    Jul 15, 2008
    #2
  3. kpua macrumors 6502

    Joined:
    Jul 25, 2006
    #3
    It's also a hint that you can use the dot notation to access the property.
     
  4. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #4
    You are getting confused between the difference in the language and the API (Cocoa). There is no such thing as Cocoa 2.0, on the other hand there is an Objective-C 2.0.

    It may sound like nit picking but they are two different things.
     
  5. mongrol macrumors regular

    Joined:
    Jul 16, 2007
  6. masonmey macrumors newbie

    Joined:
    Jul 15, 2008
    #6
    Cocoa 2.0 properties

    I agree with mongrol (and the cocoa is my girlfriend blog) but it is important to note that you can use the "property" declaration and still use the familiar Objective C syntax. That's one of the cool things about it. The following code would access the same class property:

    Code:
    	
    [self setName:@"Mason"];
    NSLog(@"%@", [self name]);
    self.name = @"Meyer";
    NSLog(@"%@", self.name);
    
     
  7. Catfish_Man macrumors 68030

    Catfish_Man

    Joined:
    Sep 13, 2001
    Location:
    Portland, OR
    #7
    I agree about the dot syntax being fairly useless (or even a slight detriment), but properties themselves are great. Why? Because they put more information about the actual semantics of the method in the header.

    Code:
    -(void)setFoo:(NSBar*)bar; 
    -(NSBar *)foo;
    @property(copy, readwrite, nonatomic) NSBar *baz;
    
    Quick, which one(s) require the argument to conform to NSCopying? Which one(s) update atomically? Which one(s) retain their argument?
     
  8. HiRez macrumors 603

    HiRez

    Joined:
    Jan 6, 2004
    Location:
    Western US
    #8
    I thought I was going to love the dot syntax but yeah, it's ugly and not really particularly easier either. The only thing I do like about it over using the brackets is that sometimes I start typing a big nested statement and I often find myself having to go back over the line when I'm done to add, remove, or move brackets around. Dot syntax does give you a nice clean way of walking down a property path (myCar.interior.dashboard.speedometer.needle). But it's not worth the mess, I just decided to skip it and use the old way.

    As for properties in general, I think they really screwed up how you type them in. I thought "This will be great, I'm so tired of typing all the accessors and copying things from .h to .m and back and so on". But between the declaration of the variable, the @property, and the @synthesize, you still end up typing the same name 3 times. Sure you save a little typing on the actual accessor code, but why couldn't they just make one line?

    @property (readwrite, retain) NSString *name;

    Isn't the compiler smart enough to figure out what to do with that, and make the redundant variable definition and @synthesize steps for you?

    EDIT: Also, why didn't they take the dot syntax all the way and allow you to call methods with it:

    val = myObject.calculate();

    or with arguments:

    val = myObject.calculate(withRadius:myCircle.radius);
     
  9. Catfish_Man macrumors 68030

    Catfish_Man

    Joined:
    Sep 13, 2001
    Location:
    Portland, OR
    #9
    On the 64 bit runtime you don't need the variable declaration.
     
  10. aLoC macrumors 6502a

    Joined:
    Nov 10, 2006
    #10
    I don't use the dot syntax either, I think it should be explicit that you are sending a message, not just indexing in to some memory. Aaron Hillegas even takes a swipe at it in the latest edition of his book.
     
  11. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #11
    I don't want to even know that you can use dot notation. It seems to really muddle things up. You can only have pointers to Objects, you can't have a local one (I don't think, please correct me if I'm wrong) in Objective-C. As such, you could have:
    Code:
    struct NeatStruct myNeatStruct;
    struct NeatStruct *myNeatStructPtr = &myNeatStruct;
    NeatClass *myNeatClass = [[NeatClass alloc] init];
    
    x = myNeatStruct.fieldA;
    y = myNeatStructPtr->fieldB;
    z = myNeatClass.FieldA;
    
    So when you have a pointer to a struct you use the structure pointer, ->. When you have a local copy of a struct you use the dot operator to get at a field. When you have a pointer to an Object, you use the dot? Erm, that doesn't sit too well with me. I'm not saying I would be happier if the syntactic sugar was -> instead of ., but it just seems to muddle things up.

    Sure, you may never create a struct, but there might be some floating out there you have to interact with. I'd rather stick with square brackets.

    And the mutator synthesis makes me even more uncomfortable:
    Code:
    NeatClass *myNeatClass = [[NeatClass alloc] init];
    myNeatClass.FieldA = 5;
    is sugar for:
    Code:
    NeatClass *myNeatClass = [[NeatClass alloc] init];
    [myNeatClass setFieldA:5];
    ?

    That just seems unfortunate.

    -Lee

    P.S. Things like NSPoint and other Foundation data types are structs that you might need to interact with, so it does happen in the cocoa world. I thought a real world example might be nice.
     
  12. masonmey macrumors newbie

    Joined:
    Jul 15, 2008
    #12
    Just to clarify the situation:

    I think the dot syntax has gotten artificially involved with the property declaration (probably because Apple tends to introduce them together). The property declaration tells the compiler to create the accessor methods for you. This means less code for you to write and less bugs. The property declaration is new syntax or "sugar". It may ease the transition from other languages for some people. There is a pretty good article on the developer site (http://developer.apple.com/leopard/overview/objectivec2.html). You can use the dot syntax for properties but it is not required.
     
  13. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #13
    I have nothing at all against the property declaration, it's a great addition. The dot notation is what I think causes problems because of an inconsistency with other uses of the notation in C.

    -Lee
     
  14. lazydog macrumors 6502a

    Joined:
    Sep 3, 2005
    Location:
    Cramlington, UK
    #14
    An object is implied by its name, ie you send an object a message by [ myNeatClass message ] and not [ *myNeatClass message ], so I guess the . syntax is consistent with this.

    b e n
     
  15. nacho4d thread starter macrumors newbie

    Joined:
    Jul 13, 2008
    #15
    Thanks to everyone!

    thanks for your reply.I got it.
    And, personally, I got confused with dots, because at first glance I thought they where using some kind of structures like in Core Graphics. But then reading more I realized that they where actually Objects and then the same as you.:D
    (By the way sorry for the Cocoa 2.0 mistake, it should be Objective-C 2.0)
     
  16. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #16
    true, but there is not a message pass operator in C, and the first operand is always a pointer, so this doesn't seem as ambiguous to me.

    -Lee
     

Share This Page