0

I have a simple NSObject which I'm using a Singleton within:

.h file:

#import <Foundation/Foundation.h>

@interface FooAPIClient : NSObject

+ (FooAPIClient *) sharedInstance;

@end

and my .m file:

#import "FooAPIClient.h"

@implementation FooAPIClient

+ (FooAPIClient *) sharedInstance {
    static FooAPIClient *_sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _sharedInstance = [[FooAPIClient alloc] init];
    });
    return _sharedInstance;
}

@end

I'm trying to work out where I can put my properties, ie. @property(nonatomic, strong) NSString *bar; without putting them in my header file. If I copy @interface into my .m file, it complains that I'm duplicating my definition.

Could do with a couple of pointers on where to declare internal (to the Class) properties?

benhowdle89
  • 36,900
  • 69
  • 202
  • 331

1 Answers1

0

I think you are looking for class extension.

In your .m file, put these lines before your @implementation

@interface FooAPIClient()

/// declare your "private" properties here

@end
Casey
  • 103
  • 6
  • Ah ace! I could definitely go read about this, but is this the primary purpose of a class extension? And also, why wouldn't Xcode create a new class with this already in it's boilerplate code? – benhowdle89 Aug 19 '14 at 15:53
  • () is a marker for extension, it's not a declaration for new interface. Like `@implementation`, it will look for the corresponding @interface declared in your code. Extension can be used for overriding properties declared in header or you can declare 'private' methods and properties which you want to hide from other classes. This [website](http://code.tutsplus.com/tutorials/objective-c-succinctly-categories-and-extensions--mobile-22016) explains the usage of class extensions and category. – Casey Aug 19 '14 at 16:09