0
votes

So here's what I know about properties in Objective-C. Please correct me if these are not facts.

  1. When declaring a property you are declaring the setter/getter for a instance variable
  2. If you want to have the setter and getters defined you need to synthesize them
  3. If you synthesize, the instance variable is defined for you. Best practice is to rename the iVar so that the getter and iVar aren't the same name. So you usually do: @synthesize myVar = _myVar

All of my knowledge about properties is coupled with instance variables. I've watched some videos recently that say properties can be used for other instance methods besides setters/getters.

Is this true? If so, how and why would you use a property in this way? For instance I was watching a Stanford cs193p video about protocols and it said that you could have a prototype in a protocol. I could of misunderstood.

Anyways thanks to those who respond

3
What did you mean by "properties can be used for other instance methods besides setters/getters"?Basem Saadawy
Well, setters and getters are instance methods. Any other instance methods that are provided via the public API.Oscar

3 Answers

2
votes

When declaring a property you are declaring the setter/getter for a instance variable

No, you are declaring a getter and possibly a setter of a property. Period. Declaring a property does not itself imply an instance variable. There are many ways to implement a property. Instance variables happen to be a common and popular way, but non-ivar properties are very common.

If you want to have the setter and getters defined you need to synthesize them

No. (As sergio points out, I originally confused "defined" and "declared.") Almost. The @property line itself declares the setter and getter. If you want to have the setter and getter implemented for you, that is called "synthesize," but you no longer need to do this manually. The complier will automatically create a getter and setter for any property that you declare but do not implement (unless you explicitly ask it not to using @dynamic).

If you synthesize, the instance variable is defined for you. Best practice is to rename the iVar so that the getter and iVar aren't the same name. So you usually do: @synthesize myVar = _myVar

Almost. This was true a few months ago, but you no longer need to actually do that @synthesize. It will be done automatically for you by the compiler now.

This header:

@interface MyObject : NSObject
@property (nonatomic, readwrite, strong) NSString *something;
@end

is almost the same as this header:

@interface MyObject : NSObject
- (NSString *)something;
- (void)setSomething:(NSString *)something;
@end

There are some very small differences between these two, some related to the runtime and some related to the compiler, but it is clearer if you just pretend they're identical.

All you're doing in both of these cases is declaring some methods. You are not declaring how they're implemented. You're not declaring ivars. You're just declaring methods. You are now free to implement those methods any way you like. If you like, you can implement them by letting the compiler synthesize some default implementations for you. If you like you can implement them by hand. You can do one of each if you like.

1
votes

Properties are synthesized by default since Xcode 4.4. So you only need to declare the property (myVar). There will also be a _myVar available that you may use instead of accessing self.myVar.

0
votes

Using a properties as a parameterless methods is torsion them into a something they are not.