Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing NSMutableDictionary as backing store for properties
    text
    copied!<p>I am looking for a shorthand way of setting my properties directly to an NSMutableDictionary that is a instance variable. ie:</p> <p>KVCModle.h:</p> <pre><code>@interface KVModel : NSObject { NSMutableDictionary * data; } @property(nonatomic,assign)NSString * string1; @property(nonatomic,assign)NSString * string2; @end </code></pre> <p>KVCModel.m</p> <pre><code>#import "KVModel.h" @implementation KVModel -(id)init { self = [super init]; if(self) { data = [[NSMutableDictionary alloc] init]; } return self; } -(NSString *)string1 { return [data objectForKey:@"string1"]; } -(NSString *)string2 { return [data objectForKey:@"string2"]; } -(void)setString1:(NSString *)_string1 { [data setObject:_string1 forKey:@"string1"]; } -(void)setString2:(NSString *)_string2 { [data setObject:_string2 forKey:@"string2"]; } -(void)dealloc { [data release]; [super dealloc]; } @end </code></pre> <p>I have tried to override <code>setValue:ForKey:</code> and <code>valueForKey:</code>, but those aren't called, they allow you to directly set properties without using the property syntax.</p> <p>I have made preprocessor macros to make this work in the past, but I am not interested in typing at all, and would like to avoid as much of it as I can in the future. Is there a way to make this work that I am not familiar with?</p> <p>I have thought about using NSManagedObject, but I am not sure if I can get what I want out of that. <strong>EDIT:</strong> <a href="http://objectivelybetter.com/tests/keyValueTest.zip" rel="nofollow" title="source">source</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload