Reason for using variables instead of properties?


#1

I’m curious why when creating the outlets on the DetailViewController we are instructed to use instance variables instead of properties. Is this something covered later? If so, I’ll just wait until I get to the explanation. (It’s also entirely possible I just forgot a previous explanation…) If not, why is that?


#2

Here, the instance variables aren’t declared with property accessors because no other class is going to access them. They are used internally by the DetailViewController, and the DetailViewController doesn’t want anyone else coming in an messing with them.


#3

Coming from the Java world, I think that’s where we’d use the private access modifier on accessors. Am I right to assume there is no such notion as scope modifiers in Obj-C? (Furthermore, I would assume to do something similar you’d declare the instance variable in the header file, but then create accessors in the implementation file, which would be usable inside the class but not visible to any other class importing the header. Correct?)


#4

There are visibility modifiers for instance variables, but technically not for properties (or any methods, for that matter).

You can declare properties in the .m file in a special area called a class extension. This makes the property invisible to outside classes, but the method is still defined. There is no compiler/runtime check to say “This method is a private method, it can only be used by the class that defined it.” However, ARC “accidentally” pulls this off by disallowing messages to be sent when the file can’t see the method declaration.

// A class extension in a .m file
@interface MyClass ()
@property (nonatomic, assign) int foo;
@end
@implementation MyClass
@synthesize foo; // also creates instance variable named foo
...
@end

The choice to use direct ivar access vs. private accessors is largely a style thing. In my opinion, if there is no reason for the accessor method, then there doesn’t need to be one. Here are two things that go beyond style:

  1. When an accessor method needs to do more than simply an assignment or return a value (but still are private to the class), you should create and use a private property.
  2. When an instance variable is accessed frequently inside a class, you should avoid using a property to avoid the performance overhead of message sending.

Overall, it’s up to you. As the popularity of Objective-C grows, I see more and more rules and conventions from other languages being applied (like strict visibility modifiers), yet we did pretty good without these things in application-level code for a long time. Objective-C has largely been a “go with the conventions and things just work out okay” language without a lot of syntax to support those conventions. This is starting to change, I fear, so you wouldn’t be wrong to property-up every instance variable .


#5

That’s one of the reasons I’m asking… I’m very used to conventions from other languages, and I want to make sure I stick to the proper ones for this language. Thanks for the info!

(PS: It’s a little annoying I can’t key an ellipsis by using opt-; as it appears phpBB doesn’t like it.)