From d9d15b3f8729758f19b21918fee758b856921673 Mon Sep 17 00:00:00 2001 From: Joe Wreschnig Date: Thu, 28 Feb 2013 13:46:41 +0100 Subject: [PATCH] Use a weak reference for TargetConfig configurations to avoid a circular reference, and to auto-nil them when the chosen config is deleted. --- ConfigsController.m | 10 ---------- TargetConfig.h | 12 ++++++------ TargetConfig.m | 33 +++++++++++++++------------------ 3 files changed, 21 insertions(+), 34 deletions(-) diff --git a/ConfigsController.m b/ConfigsController.m index 50861cf..65732e2 100644 --- a/ConfigsController.m +++ b/ConfigsController.m @@ -73,16 +73,6 @@ if (toRemove == manualConfig) manualConfig = configs[0]; - // remove all "switch to configuration" actions - for (Config *config in configs) { - NSMutableDictionary *entries = config.entries; - for (id key in entries) { - Target *target = entries[key]; - if ([target isKindOfClass:[TargetConfig class]] - && [(TargetConfig *)target config] == toRemove) - [entries removeObjectForKey: key]; - } - } [(ApplicationController *)[[NSApplication sharedApplication] delegate] configsChanged]; [tableView reloadData]; } diff --git a/TargetConfig.h b/TargetConfig.h index b08e31b..addf2eb 100644 --- a/TargetConfig.h +++ b/TargetConfig.h @@ -7,14 +7,14 @@ // #import + +#import "Target.h" + @class Config; -@class Target; -@interface TargetConfig : Target { - Config *config; -} +@interface TargetConfig : Target -@property(readwrite, strong) Config* config; -+(TargetConfig*) unstringifyImpl: (NSArray*) comps withConfigList: (NSArray*) configs; +@property (weak) Config *config; ++ (TargetConfig *)unstringifyImpl:(NSArray *)comps withConfigList:(NSArray *)configs; @end diff --git a/TargetConfig.m b/TargetConfig.m index 8e4fc40..469edab 100644 --- a/TargetConfig.m +++ b/TargetConfig.m @@ -7,30 +7,27 @@ #import "TargetConfig.h" - @implementation TargetConfig -@synthesize config; - --(NSString*) stringify { - return [[NSString alloc] initWithFormat: @"cfg~%@", [config name]]; +- (NSString *)stringify { + return [[NSString alloc] initWithFormat: @"cfg~%@", self.config.name]; } -+(TargetConfig*) unstringifyImpl: (NSArray*) comps withConfigList: (NSArray*) configs { - NSParameterAssert([comps count] == 2); - NSString* name = comps[1]; - TargetConfig* target = [[TargetConfig alloc] init]; - for(int i=0; i<[configs count]; i++) - if([[configs[i] name] isEqualToString:name]) { - [target setConfig: configs[i]]; - return target; - } - NSLog(@"Warning: couldn't find matching config to restore from: %@",name); - return NULL; ++ (TargetConfig *)unstringifyImpl:(NSArray *)comps withConfigList:(NSArray *)configs { + NSString *name = comps[1]; + TargetConfig *target = [[TargetConfig alloc] init]; + for (Config *config in configs) { + if ([config.name isEqualToString:name]) { + target.config = config; + return target; + } + } + NSLog(@"Warning: couldn't find matching config to restore from: %@", name); + return nil; } --(void) trigger { - [[(ApplicationController *)[[NSApplication sharedApplication] delegate] configsController] activateConfig:config]; +- (void)trigger { + [[(ApplicationController *)[[NSApplication sharedApplication] delegate] configsController] activateConfig:self.config]; } @end -- 2.30.2