4

I'm a beginner to Objective-C, coming over from Swift. It seems as if there are two different @interface instances in which I can declare my ivars. One in my header file, such as this:

// Header file
@interface ViewController : UIViewController
{
   // declare instance variables
}

@end

And another that I can add in my implementation file, such as this:

// Implementation file
@interface ViewController ()
// declare instance variables
@end

@implementation ViewController

@end

EDIT: I was learning the "old way" of doing things, which taught me to declare private ivars in the .h file. My confusion stemmed from seeing ivars declared in .h (old way) as well as in .m (new, preferred way). Here's what I've learned (so far...):

// .h
    @interface SomeClass : UIViewController

    @property (nonatomic, assign) BOOL someBool;
       // declare other properties, which will all be public
    @end

// .m
    @interface SomeClass () <UITextFieldDelegate>
       // what do I declare here?...
    @end

    @implementation SomeClass {
      // ...and what do I declare here?
    }

    // method implementations

    @end

However, I'm still confused as to the difference between my .m's @interface and the @implementation curly brackets. @matt said to never use curly brackets, but @rmaddy's answer here suggests that @implementation SomeClass {} is ok. So, which is it?

Community
  • 1
  • 1
slider
  • 2,736
  • 4
  • 33
  • 69
  • 2
    matt didn't say to never use curly braces. He suggested to never use ivars but to always use properties. That aside, if you choose to use ivars (which many people do), then they should go in the `{ }` of the `@implementation` in the .m as discussed in the duplicate question's accepted answer and my answer you linked in your updated question. – rmaddy May 02 '16 at 04:25
  • 2
    The `@interface ()` (known as the class extension) in the .m should be for private properties and private protocols. The `@implementation { }` block should be for private ivars. – rmaddy May 02 '16 at 04:36
  • That makes sense. Now, what is the difference between a private property and an instance variable? I was under the assumption that there was only such thing as a public property, which led me to believe that private properties and ivars are equivalent. – slider May 02 '16 at 04:39
  • 1
    An instance variable is just that, a variable created on behalf of an instance of the class. A property is a getter method and (optionally) a setter method with defined semantics (atomic/non-atomic, memory management, etc.). Typically, a property is backed by a synthesized ivar but it doesn't have to be. In either case, being private or public is a completely separate aspect that simply defines who can see the ivar or property. – rmaddy May 02 '16 at 04:43
  • @rmaddy isn't that ivars do have getters and setters. I mean otherwise how do we get and set the value of an ivar? Only that with properties we have more easier/abstract control over its atomicity, memory management and read/write – mfaani May 02 '16 at 19:46
  • 1
    @asma22 No, ivars have no setter or getter of any kind. It is just a variable. The variable is accessed like any other variable. There is no method call involved. A property is just a pair of methods. The implementation of those method define the atomicity, etc. Those methods don't even need an ivar (though typically they do use one). – rmaddy May 02 '16 at 20:00
  • @rmaddy Hah!!!! OK, I think I am getting closer into understanding ivars vs properties. ( I have read many answers many times) I know that in theory we every property is by default, atomic, readwrite, strong. But consider by default every property had **none** of the 3 mentioned. Then would there still be any difference between property and ivar? If so then what are those difference? – mfaani May 02 '16 at 20:25
  • 1
    @asma22 There is a huge difference regardless of the attributes of the property. A property is a concept. It is implemented through methods - a setter (if not readonly) and getter. An ivar, on its own, has no methods. They are two completely different concepts. Don't get confused by the fact that typically the implementation of a property happens to make use of an ivar. – rmaddy May 02 '16 at 20:38
  • @rmaddy Can you tell me how the setting and getting of a variable is implemented? (I hope I am asking something that I can understand :] ) – mfaani May 02 '16 at 21:32
  • 1
    @asma22 It's just an assignment to memory. That's it. Please don't post any more on this. Post your own question if needed. We should not have gotten so off track from this original question. – rmaddy May 02 '16 at 21:41

1 Answers1

3

Do you want it to be accessible publicly? Then write in .h file.

If you don't want other classes to see it ie you want to make it private and only visible to that class itself then write in .m file.

For more information you can see this question: Where to put iVars in "modern" Objective-C?

Community
  • 1
  • 1
mfaani
  • 33,269
  • 19
  • 164
  • 293
  • But in the case of my `.h` file, what would I declare in curly brackets? It seems like the brackets promise some sense of encapsulation whereas outside the brackets everything is public. I feel like I've seen ivars declared both ways. – slider May 02 '16 at 01:33
  • 1
    ivars should never be public. – rmaddy May 02 '16 at 02:44
  • @dperk See this http://stackoverflow.com/questions/2182439/how-to-access-public-instance-variable-from-another-class-in-objective-c. But as rmaddy has mentioned you should never use public ivars, its basically bad practice. Also the link I shared in the answer provides answer to your question. Also see http://stackoverflow.com/questions/23652665/static-const-vs-extern-const AND http://stackoverflow.com/questions/7642304/objective-c-static-extern-public-variables. Hopefully they all help you better understand – mfaani May 02 '16 at 03:38
  • thanks @rmaddy please see edited – slider May 02 '16 at 04:12