0

Assume all of the following code is written in an implementation file .Could somebody explain the difference between :

#1

    @interface ViewController ()
    @property (nonatomic) NSDictionary *currentAlbumData;
    @end

    @implementation ViewController

#2
    @interface ViewController () {
    NSDictionary *currentAlbumData;
    }
    @end

    @implementation ViewController

#3
    @interface ViewController
    @end

    @implementation ViewController {
    NSDictionary *currentAlbumData;
    }

    - some methods here - 
    @end

The way I see it, the first one declares a property variable in a class extension. The second one declares an instance variable in a class extension. The third one declares an instance variable that isn't a class extension...what does this imply? How does it compare to simply declaring an ivar in a class extension?

karan satia
  • 307
  • 4
  • 16

1 Answers1

0

The difference between the property and the instance variables should be obvious.

The difference between defining an ivar in an extension versus implementation is one of visibility. While private extensions such as you included in your post are usually written in the same file as the implementation, they don't have to be. Interfaces can be defined anywhere, and all extensions visible to the compiler/linker are rolled together into one class definition at build time.

Avi
  • 7,469
  • 2
  • 21
  • 22
  • A matter of visibility to MYSELF? As in I can see the extended variables right there in the same file, versus navigating to a different file elsewhere? Not very clear. Additionally, is it not a matter of scope (as in file scope vs global scope)? – karan satia Jan 13 '16 at 16:29
  • An instance variable defined in an `@interface` section is visible to any code that sees it. It doesn't matter if that `@interface` is an extension or the primary one. – Avi Jan 13 '16 at 17:07