https://github.com/rannie/rhfirmmodel
Objective-C Base Model Class.
https://github.com/rannie/rhfirmmodel
Last synced: 8 months ago
JSON representation
Objective-C Base Model Class.
- Host: GitHub
- URL: https://github.com/rannie/rhfirmmodel
- Owner: Rannie
- License: mit
- Created: 2015-01-09T10:01:49.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2015-01-09T15:08:13.000Z (over 10 years ago)
- Last Synced: 2025-01-02T02:43:23.045Z (9 months ago)
- Language: C
- Homepage:
- Size: 168 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# RHFirmModel
============================A lightness base model.
Thanks to Mantle and JSONModel.
RHFirmModel support nested dictionary converted into model.Like a user has a address property which class is RHFirmModel's subclass.It also can make the value to be a model class instance.##Feature
- [x] Model ---> Dictionary (Specify Keys or Whole)
- [x] Model <--- Dictionary (By KVC)
- [x] Model ---> JSONString
- [x] Model <--- JSONString
- [x] Model <--> Model (Merge)
- [ ] Model Storage##Usage
1. Dictionary to Model
+ (instancetype)modelWithDictionary:(NSDictionary *)dictionary;
+ (instancetype)modelWithDictionary:(NSDictionary *)dictionary error:(NSError **)error;
- (instancetype)initWithDictionary:(NSDictionary *)dictionary error:(NSError **)error;
If i have dict object like this:
@{
@"id": @"4248292",
@"name": @"ran",
@"sex": @"female",
@"age": @"24",
@"address": @{
@"city": @"beijing",
@"street": @"wangjing",
@"postcode": @"200000"
}
}
Use
User *user = [User modelWithDictionary:userDict]
Set values to a User instance.
2. Model to Dictionary@property (nonatomic, copy, readonly) NSDictionary *dictionaryValue;
- (NSDictionary *)toDictionaryWithKeys:(NSArray *)propertyNames;Like this:
NSLog(@"user : %@", [user toDictionaryWithKeys:@[@"name", @"sex", @"age"]]);
Output:
user : {
age = 24;
name = ran;
sex = female;
}
3. To JSON
- (NSString *)toJSONString;
Like this:
NSLog(@"json user : %@", [user toJSONString]);
Output:
json user : {"age":"42","sex":"female","user_id":"232532","name":"liu","address":{"street":"wudaokou","city":"beijing","postcode":"200000"}}4. Merge
- (void)mergeValueForKey:(NSString *)key fromModel:(RHFirmModel *)model;
- (void)mergeValuesForKeysFromModel:(RHFirmModel *)model;
See demo or RHFirmModel interface file to know more.
##Liscence
RHFirmModel is available under the MIT license. See the LICENSE file for more info.