From: Joe Wreschnig Date: Thu, 28 Feb 2013 12:46:41 +0000 (+0100) Subject: Use a weak reference for TargetConfig configurations to avoid a circular reference... X-Git-Tag: version-1.0~100 X-Git-Url: https://git.yukkurigames.com/?a=commitdiff_plain;h=d9d15b3f8729758f19b21918fee758b856921673;p=enjoyable.git Use a weak reference for TargetConfig configurations to avoid a circular reference, and to auto-nil them when the chosen config is deleted. --- 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