-1

i was just figuring out how to have private variable with class extension. And what i noticed was that there is no default code for class extension inside a normal class. And when i try to declare variable with @property, it does not allow me but to type @protected and @private while inside ViewController, it only allow me to have @property @optional @required.

SO, my question is that is there any reason for that..? What is the difference and what makes it different..?

Avt
  • 16,927
  • 4
  • 52
  • 72
denis_choe
  • 667
  • 6
  • 15

1 Answers1

2

You didn't post any code, leaving us to infer what you're doing. I'm going to assume it's something like this:

@interface ViewController ()

@property id someProperty; // This works

@private
id someVar; // This does not work

@end

and:

@implementation ModelClass
{
  @property id someProperty; // This does not work

  @private
  id someVar; // This works
}

@end

Assuming that's true, it seems you're confused about the nature of properties vs. instance variables. @property is essentially a way to declare accessor methods, for which the compiler (by default) automatically synthesizes method implementations and a corresponding backing instance variable. Instance variables on the other hand, are simply per-instance variables, not methods at all.

The first block of code above is a class extension on ViewController. Class extensions allow you to declare additional methods -- and thereby @properties too -- separately from the main/public interface for a class.

The braces after @implementation in the second block denote a place to declare additional instance variables (only).

@protected and @private are visibility modifiers for instance variable declarations, and control whether an instance variable is visible only to instances of the class itself (@private), instances of the class and its subclasses (@protected), or publicly (@public). These modifiers cannot be used for methods (of which @properties are a special case). Afterall, in Objective-C, methods are actually always public in the sense that it's only at runtime that a message send is turned into a method call, and the compiler can't truly enforce a limitation on calls to "private" methods.

Finally, to answer what I think is the heart of your question, you most certainly can add a class extension to your model class in order to declare additional "private" @properties and other methods. Xcode may not include one in the default new file template for non-view controllers, but that doesn't mean you can't add one yourself:

@interface ModelClass ()

@property id somePrivateProperty; // Works just fine

@end
Andrew Madsen
  • 21,309
  • 5
  • 56
  • 97
  • THank you SO much ! btw, could you explain little bit more about this sentence please..? Afterall, in Objective-C, methods are actually always public in the sense that it's only at runtime that a message send is turned into a method call, and the compiler can't truly enforce a limitation on calls to "private" methods. – denis_choe Apr 30 '14 at 14:51
  • what if i put my method declaration in that extension..? – denis_choe Apr 30 '14 at 14:52
  • Regarding that sentence: see bbum's [excellent answer](http://stackoverflow.com/a/2159027/344733). You can declare regular methods, and @properties in a class extension. – Andrew Madsen Apr 30 '14 at 15:09