forked from nicklockwood/BaseModel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseModel.h
More file actions
executable file
·120 lines (99 loc) · 3.89 KB
/
BaseModel.h
File metadata and controls
executable file
·120 lines (99 loc) · 3.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
//
// BaseModel.h
//
// Version 2.6.3
//
// Created by Nick Lockwood on 25/06/2011.
// Copyright 2011 Charcoal Design
//
// Distributed under the permissive zlib license
// Get the latest version from here:
//
// https://github.com/nicklockwood/BaseModel
//
// This software is provided 'as-is', without any express or implied
// warranty. In no event will the authors be held liable for any damages
// arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented; you must not
// claim that you wrote the original software. If you use this software
// in a product, an acknowledgment in the product documentation would be
// appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such, and must not be
// misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
#import <Foundation/Foundation.h>
extern NSString *const BaseModelSharedInstanceUpdatedNotification;
extern NSString *const BaseModelException;
typedef NS_ENUM(NSUInteger, BMFileFormat)
{
BMFileFormatKeyedArchive = 0, //default
BMFileFormatXMLPropertyList,
BMFileFormatBinaryPropertyList,
BMFileFormatJSON,
BMFileFormatUserDefaults,
BMFileFormatKeychain, //requires FXKeychain library
BMFileFormatCryptoCoding, //requires CryptoCoding library
BMFileFormatHRCodedXML, //requires HRCoder library
BMFileFormatHRCodedJSON, //requires HRCoder library
BMFileFormatHRCodedBinary, //requires HRCoder library
BMFileFormatFastCoding, //requires FastCoding library
};
//use the BaseModel class as the base class for any of your
//model objects. BaseModels can be standalone objects, or
//act as sub-properties of a larger object
@interface BaseModel : NSObject <NSCoding>
//loading sequence:
//setUp called first
//then setWithDictionary/Coder/etc if resource file exists
//then setWithDictionary/Coder/etc again if save file exists
//tearDown is called prior to dealloc (but only if setUp was called)
- (void)setUp;
- (void)setWithDictionary:(NSDictionary *)dict;
- (void)setWithCoder:(NSCoder *)decoder;
- (void)tearDown;
//new autoreleased instance
+ (instancetype)instance;
//shared (singelton) instance
+ (instancetype)sharedInstance;
+ (BOOL)hasSharedInstance;
+ (void)setSharedInstance:(BaseModel *)instance;
+ (void)reloadSharedInstance;
//creating instances from a configuration object
+ (instancetype)instanceWithObject:(id)object;
- (instancetype)initWithObject:(id)object;
+ (NSArray *)instancesWithArray:(NSArray *)array;
//creating an instance using NSCoding
+ (instancetype)instanceWithCoder:(NSCoder *)decoder;
- (instancetype)initWithCoder:(NSCoder *)decoder;
//loading and saving the model from a data file
+ (instancetype)instanceWithContentsOfFile:(NSString *)path;
- (instancetype)initWithContentsOfFile:(NSString *)path;
- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)atomically;
- (BOOL)writeToFile:(NSString *)path format:(BMFileFormat)format atomically:(BOOL)atomically;
//get model properties
+ (NSArray *)allPropertyKeys;
+ (NSArray *)codablePropertyKeys;
- (NSDictionary *)dictionaryRepresentation;
//resourceFile is a file, typically within the resource bundle that
//is used to initialise any BaseModel instance
//saveFile is a path, typically within application support that
//is used to save the shared instance of the model
//saveFormat is the preferred format to use when saving the file
+ (NSString *)resourceFile;
+ (NSString *)saveFile;
+ (BMFileFormat)saveFormat;
//save the model
- (BOOL)save;
//generate unique identifier
//useful for creating universally unique
//identifiers and filenames for model objects
+ (NSString *)newUniqueIdentifier;
@end