0

Total newbie question follows...

I am learning Objective C based Cocoa (OS X) app development. Most of the books and videos I have are iOS based, so I'm seeing how the two platforms differ.

I have converted a simple iOS app to OS X. I got the converted OS X app to work fine, but I have a question: Why doesn't OS X ViewController.m file have an @interface section?

I created the iOS app using Interface Builder, dragging UI elements from Main.Storyboard to ViewController.m, in the assistant editor. I dragged a UILabel cell into the @interface section of ViewController.m, created an instance variable @property. In iOS, the ViewController.h public @interface section remains empty.

In the OS X app, I dragged an NSTextField cell to the @interface section of the ViewController.h file to create a similar instance variable.

Is the OS X ViewController.h @interface section the correct place for IBOutlets? Doesn't this expose these instance variables to other classes? In the iOS case, since the instance variables were NOT added to the ViewController.h file, I assume they are hidden, correct? Can I make the OS X instance variables private?

As I mentioned, the OS X code works, I'm just trying to understand the differences between the platforms, and make sure I am headed in the right direction.

SDGary
  • 434
  • 3
  • 17

2 Answers2

1

Yes you are right instance variable should not added to ViewController.h file to make them private. Same like you can make instance variables as private variables in OS X . Like this`@interface MasterViewController ()

@property (nonatomic, weak) NSString* strTemp;

@end

@implementation MasterViewController

You can get difference between cocoa and cocoa touch by already posted question for this

abrar ul haq
  • 404
  • 3
  • 14
1

Before I answered I wanted to check if the same happened to me-- but when I created a custom NSViewController in a new iOS OR OS X app, both would generate the @interface in the implementation file (.m file)

enter image description here

So I'm not sure why you are seeing that.

However, to answer your question, (as you said) properties declared in the @interface in your header file (.h) are public. While properties declared in the @interface in the implementation are private. This is because the @interface inside of your implementation is called an "Extension". Which is basically just an anonymous "Category".

Unless outside objects need a reference to your outlets, then I would continue placing them inside of your implementation file. If for whatever reason one isn't generated for you, there is nothing wrong with manually typing out your own class extension to declare the properties

A O
  • 5,516
  • 3
  • 33
  • 68
  • Thank you so much for your reply. How did you get your code? I am using Xcode 6.3.2 on Yosemite. When I create a new project using File > New > Project..., then select "Cocoa Application" under OS X Application. I have "use storyboards" selected. When the new project opens, the ViewController.m has no @interface section. The Main.storyboard shows a Window Controller and a single ViewController. I am not adding any additional ViewControllers, I am just using the one that is auto-created. Are you doing something different? – SDGary Jun 17 '15 at 22:14
  • And, yes, I verified that I can manually add the @interface code. That is a simple workaround... – SDGary Jun 17 '15 at 22:14
  • I am doing the same thing-- I am not sure why you don't the extension `@interface` generated for you. Good to hear it works ^^ – A O Jun 18 '15 at 03:30
  • OK. Thanks for confirming. I'll try to figure out why my ViewController.m file differs from yours in a separate question. – SDGary Jun 18 '15 at 23:51