Reputation:
@interface OuterSpaceController ()
//Cannot alloc and init my array here for some reason
NSMutableArray *spaceObjectsAsPropertyLists = [[NSMutableArray alloc] init];
@end
//But could do it here... Can someone explain why this is the case?
NSMutableArray *spaceObjectsAsPropertyLists = [[NSMutableArray alloc] init];
@implementation OuterSpaceController
@end
Hi I had a question regarding this code structure in objective-C. My first question is why is this portion even present in the implementation file?:
@interface OuterSpaceController ()
@end
I tried creating my NSMutableArray there^^^, so I can access it in all my methods in the implementation file, but I was not able to for some reason. Also regarding my NSMutableArray if I create it in between the @end and @implementation OuterSpaceController lines of code (like shown in my first block of code), will my NSMutableArray be allocated and initialized every time my view controller is loaded in memory? And if not when does the allocation and initialization of this NSMutableArray *spaceObjectsAsPropertyLists Mutable Array happen?
Thank you so much for the help in advance!
Upvotes: 0
Views: 67
Reputation: 6068
This is called a class extension:
@interface OuterSpaceController ()
@end
Extensions allow you to add declarations to your class. See Apple's docs for details. This is mostly used for declaring properties or methods in a different scope of the original declaration. Something like this:
// Foo.h
@interface Foo
@property (strong) NSArray * everyoneCanSeeThis;
@end
// Foo.m
@interface Foo ()
@property (strong) NSArray * thisIsOnlyVisibleInThisFile;
@end
There are other uses to extensions. I recommend you read Apple's docs.
Your other question is not related to this. In order to initialise the spaceObjectsAsPropertyLists
property you have two options. One, when initialising the class:
@interface OuterSpaceController ()
// Does not initialise, just declares. This is an interface, not an implementation
@property (strong) NSMutableArray *spaceObjectsAsPropertyLists;
@end
@implementation OuterSpaceController
- (instancetype)init
{
self = [super init];
if (self) {
_spaceObjectsAsPropertyLists = [NSMutableArray array];
}
return self;
}
@end
In this case the array is instantiated as soon as the class is initialised, but there's a second option. There's another way of initialising classes called Lazy Initialisation. Here's how it goes:
@interface OuterSpaceController ()
// Does not initialise, just declares. This is an interface, not an implementation
@property (strong) NSMutableArray *spaceObjectsAsPropertyLists;
@end
@implementation OuterSpaceController
- (instancetype)init
{
// Do not instantiate!
return [super init];
}
- (NSMutableArray *)spaceObjectsAsPropertyLists
{
if (_spaceObjectsAsPropertyLists == nil) {
_spaceObjectsAsPropertyLists = [NSMutableArray array];
}
return _spaceObjectsAsPropertyLists;
}
@end
The general idea is to override the property's getter. The upside of this method is that the property will be initialised only as soon as it is needed, and not sooner. This method is usually more memory friendly.
You need to know that your interfaces can only contain declarations. It's the implementation section who's responsible for pretty much everything else.
Upvotes: 2