0

info: iOS5, xcode4.3.2, iphone5

Create tab view controller application from xcode template wizard.
Following code gets generated (some truncated by me for this post).

=== SUFirstViewController.h

#import <UIKit/UIKit.h>    
@interface SUFirstViewController : UIViewController    
@end

=== SUFirstViewController.m

    #import "SUFirstViewController.h"    

    @interface SUFirstViewController ()    
    @end

    @implementation SUFirstViewController    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
    }    
    @end

===
My question is regarding this code snippet:

@interface SUFirstViewController ()    
@end

Why is this particular code snippet generated in the SUFirstViewController.m ?
Can I remove it ?
How do I use it ?

user77115
  • 5,517
  • 6
  • 37
  • 40

3 Answers3

1

It is generated for the case, you want do declare private things (methods, members, protocols, etc.) If you do not, you can delete it safely.

Matthias
  • 8,018
  • 2
  • 27
  • 53
1

That's an Objective-C Class Extension. It's where you typically declare your private methods and instance variables. For example:

@interface MyClass()
{
    int _privateInstanceVariable;
}
@property (nonatomic, retain) NSString* privateProperty;
-(void)privateMethod;
@end

@implementation MyClass
@synthesize privateProperty;

// etc...

You can remove it if it's not used.

Darren
  • 25,520
  • 5
  • 61
  • 71
1

As Matthias said, is for things you want to keep private or hidden to clients of the class :)

This part of the code is called class extension (others call them anonymous categories because they don't have a name in the parenthesis) and you could used them to write something like this:

@interface SUFirstViewController ()
@property (nonatomic, strong) IBOutlet UITextView *textview;
@property (nonatomic) float privateProperty;
- (IBAction)someAction:(id)sender;
- (float)resultOfDoingSomething;
@end

Interface Builder is pretty smart and you will be able to hock your outlets/actions if you write them here, in fact Apple recommends to have your IBOutlets/IBActions declared in this place because most of the time you don't need to expose them.

You can also have any other private property, and method like the ones I wrote.

Hope it helps.

nacho4d
  • 43,720
  • 45
  • 157
  • 240