When would I use this (im)mutable property pair setup? (Misunderstanding advanced KVO)
When would I use this (im)mutable property pair setup? (Misunderstanding advanced KVO)
- Subject: When would I use this (im)mutable property pair setup? (Misunderstanding advanced KVO)
- From: Daryle Walker <email@hidden>
- Date: Fri, 15 Aug 2014 19:24:43 -0400
As I’m stripping out the NSDocument stuff, I realize that I need some sort of global object to hold each window(-controller) as I create them, otherwise ARC will purge them soon after creation. The application delegate seems like a good option, as it’s my only other custom class and Apple uses it to store the window in sample projects with a single window. I read a neat technique at <http://eschatologist.net/blog/?p=189>, and I’m wondering if I would ever need it and should switch to something easier.
//==In the header file===
@interface PrairieAppDelegate : NSObject <NSApplicationDelegate, NSOpenSavePanelDelegate>
// unrelated declarations...
@property (nonatomic, copy) NSSet * windowControllers;
@property (nonatomic, readonly) NSMutableSet * mutableWindowControllers;
@end
//=====
I put the implementation methods in the source file:
//==In the implementation file===
#pragma mark Private interface
@interface PrairieAppDelegate () {
NSMutableSet * _windowControllers;
}
@end
@implementation PrairieAppDelegate
#pragma mark Initialization
- (id)init
{
if (self = [super init]) {
_windowControllers = [[NSMutableSet alloc] init];
}
return self;
}
// No explicit dealloc.
#pragma mark Property getters & setters
// trim unrelated property accessors...
- (NSSet *)windowControllers
{
return [NSSet setWithSet:_windowControllers];
}
- (void)setWindowControllers:(NSSet *)windowControllers
{
[_windowControllers setSet:windowControllers];
}
- (void)addWindowControllersObject:(PrBrowserController *)controller
{
[_windowControllers addObject:controller];
}
- (void)addWindowControllers:(NSSet *)controllers
{
[_windowControllers unionSet:controllers];
}
- (void)removeWindowControllersObject:(PrBrowserController *)controller
{
[_windowControllers removeObject:controller];
}
- (void)removeWindowControllers:(NSSet *)controllers
{
[_windowControllers minusSet:controllers];
}
- (void)intersectWindowControllers:(NSSet *)controllers
{
[_windowControllers intersectSet:controllers];
}
@synthesize mutableWindowControllers = _windowControllers;
#pragma mark NSApplicationDelegate overrides
//…
//=====
My window-controller class instances put themselves in the set on “init” and remove themselves on “dealloc”. I originally had “mutableWindowControllers" private to the implementation file, but the window-controller instances couldn’t insert themselves with only “windowControllers" public. That makes sense if I had an actual NSSet; but then what are these mutator methods actually supposed to do? When would they ever be called? Do I have to move them to the header file?
Should I get rid of them and stick to just a mutable-set property? (Delete the current “windowControllers” and rename the mutable variant.) But the text seemed like that would mess up KVO in a different way.
—
Daryle Walker
Mac, Internet, and Video Game Junkie
darylew AT mac DOT com
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden