0

Newbie question follows...

I am learning Objective C based OS X Cocoa app development. Most of the books and videos I have are for iOS, so I am converting some simple iOS code examples to OS X.

When I create a new OS X "Cocoa Application" project, with the "use Storyboards" box checked, the default ViewController.m in my newly created project does NOT have an @interface section. Is this expected?

A reply to my recent question Cocoa ViewController.m vs. Cocoa Touch ViewController.m indicates that another user's default ViewController.m DOES have an @interface section.

Currently, I am manually typing the @interface section for IBOutlets. Is that what others are doing? Or do I have some configuration issue?

I am using Xcode 6.3.2 on Yosemite.

Here is my default ViewController.m

//
//  ViewController.m
//  testME
//
//  Created by ME on 6/19/15.
//  Copyright (c) 2015 ME. All rights reserved.
//

#import "ViewController.h"

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    // Do any additional setup after loading the view.
}

- (void)setRepresentedObject:(id)representedObject {
    [super setRepresentedObject:representedObject];

    // Update the view, if already loaded.
}

@end
Community
  • 1
  • 1
SDGary
  • 434
  • 3
  • 17

1 Answers1

1

Typically the interface for a class (ViewController in your case) is in the header file (.h).

However some developers use the convention of putting a class extension at the top of implementation files as a way of "faking" private methods (which Objective C doesn't have.)

So you could see this in a .m file:

//The parenthesis here indicate a class extension.
@interface ViewController ()

//Only the ViewController class sees this method.
-(void) method;

@end

@implementation ViewController

-(void) method{
    //Do stuff here 
}

@end

This is not specific to iOS or MacOS but rather Objective C. You can see more about Objective C class extensions here.

The default Xcode project does not add a class extension for the created ViewController class. However if you create a new NSViewController subclass (by going to File->New->File->Cocoa Class, then create a class that is a subclass of NSViewController, you will notice the new NSViewController subclass will have a class extension generated at the top of the implementation file. This however is not required or necessary, just used as a way of defining the closest thing Objective C allows to a private interface.

You can also check out this answer for more details about implementing psuedo-private methods.

Community
  • 1
  • 1
LOP_Luke
  • 3,150
  • 3
  • 22
  • 25
  • Thanks for the detailed explanation, and for the two links. Both are very helpful. I used the class extension to make an IBOutlet instance variable private. Also, I created a new view controller, and as you indicate, it does have an empty class extension @interface. So why doesn't the default ViewController have one? Is there a valid reason why this "top level" ViewController shouldn't have an empty class extension category? – SDGary Jun 19 '15 at 23:31
  • Did users *always* create their own ViewControllers prior to Storyboards? Or do users typically delete the default ViewController, after creating a custom-named one? – SDGary Jun 19 '15 at 23:33
  • There isn't any reason why the initial view controller doesn't have the class extension. The Xcode developers probably just forgot to put it in the generated code for an initial project. And yes I can say at least from personal experience I always delete or at least rename the default ViewController to a more descriptive name. Even with storyboards you will still create your own custom view controller subclasses as your project gets more complicated. – LOP_Luke Jun 20 '15 at 14:36