DejalObject is an abstract data model class that can represent subclasses as dictionary or JSON data for saving to disk or over the network.
Included are DejalColor, DejalDate and DejalInterval concrete subclasses.
They work on both OS X and iOS.
NSObject
that adds methods to represent the receiver as a dictionary or JSON data, load default values, track changes, enumerate an array of DejalObject
instances, and more.DejalObject
to represent a color (for OS X or iOS), enabling it to be stored in a DejalObject
subclass.DejalIntervalPicker
project for OS X).A demo project is included, showing a subclass of DejalObject
to store various data types.
Include at least DejalObject.h and DejalObject.m in your project. Include the DejalColor
, DejalDate
and/or DejalInterval
files if those are needed.
Add a new class that inherits from DejalObject
.
In the header, you only need to define properties to store, e.g.:
@interface Demo : DejalObject
@property (nonatomic, strong) NSString *text;
@property (nonatomic) NSInteger number;
@property (nonatomic, strong) DejalColor *label;
@property (nonatomic, strong) DejalDate *when;
@end
In the implementation, override -initWithCoder:
and -encodeWithCoder:
if you need to support coding (secure coding is supported in DejalObject
).
Override -loadDefaultValues
to populate default values to each of the properties. A version number can be assigned (via DejalObject
’s version
property) to enable upgrading later, e.g.:
- (void)loadDefaultValues;
{
[super loadDefaultValues];
self.version = DemoVersion;
self.text = @"Foo";
self.number = 12345;
self.label = [DejalColor colorWithColor:[NSColor blueColor]];
self.when = [DejalDate dateWithNow];
}
Finally, override -savedKeys
to indicate which properties should be automatically included in the dictionary or JSON representation, e.g.:
- (NSArray *)savedKeys;
{
return [[super savedKeys] arrayByAddingObjectsFromArray:@[@"text", @"number", @"label", @"when"]];
}
A DejalObject
subclass can be represented as a NSDictionary
simply by invoking the dictionary
property on it, or as JSON via the json
property. That can then be saved to disk or the user defaults, or passed over the network.
Those properties can also be set, or a new instance can be created via +objectWithDictionary:
or +objectWithJSON:
, or an instance with default values via +object
.
DejalObject
instances automatically track changes, with a hasChanges
BOOL property indicating that something has changed (so may need to be saved). There is also a hasAnyChanges
property that recursively enumerates the properties and any other DejalObject
instances in them, e.g. if the color is changed in the above example.
After saving, you should invoke -clearChanges
to reset the change flag.
You can get the code and more information from the Dejal Open Source page.