Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

nacho4d

macrumors newbie
Original poster
Jul 13, 2008
23
0
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.
 

kpua

macrumors 6502
Jul 25, 2006
294
0
It's also a hint that you can use the dot notation to access the property.
 

Cromulent

macrumors 604
Oct 2, 2006
6,816
1,101
The Land of Hope and Glory
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.
 

masonmey

macrumors newbie
Jul 15, 2008
8
0
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);
 

Catfish_Man

macrumors 68030
Sep 13, 2001
2,579
2
Portland, OR
Interesting perspective on properties here. I tend to agree with it.

http://www.cimgf.com/2008/07/08/a-case-against-dot-syntax/

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?
 

HiRez

macrumors 603
Jan 6, 2004
6,265
2,630
Western US
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);
 

aLoC

macrumors 6502a
Nov 10, 2006
726
0
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.
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
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.
 

masonmey

macrumors newbie
Jul 15, 2008
8
0
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.
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
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.

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
 

lazydog

macrumors 6502a
Sep 3, 2005
709
6
Cramlington, UK
Code:
struct NeatStruct myNeatStruct;
struct NeatStruct *myNeatStructPtr = &myNeatStruct;
NeatClass *myNeatClass = [[NeatClass alloc] init];

x = myNeatStruct.fieldA;
y = myNeatStructPtr->fieldB;
z = myNeatClass.FieldA;

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
 

nacho4d

macrumors newbie
Original poster
Jul 13, 2008
23
0
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)
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
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

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
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.