YYModel Documentation
repository·master·Indexed 26 days ago
https://github.com/ibireme/yymodelA high-performance, lightweight model framework for iOS and macOS that converts between JSON and Objective-C models. Features include support for custom property mapping via modelCustomPropertyMapper, nested model handling, container property configuration, and property whitelists/blacklists. Provides methods for custom data validation and transformation, as well as helpers for implementing Coding, Copying, Hashing, Equality, and Description protocols.
What's inside YYModel
- To install YYModel using Carthage, add the repository to your Cartfile, update, and add the framework to your project.
Handle nested models
masterYYModel automatically handles nested models. If a property in your model is another object type that also implements YYModel conversion, the conversion will happen recursively without additional configuration.Install YYModel via CocoaPods
masterTo install YYModel using CocoaPods, add the following line to your Podfile and run the install command.Implement custom data validation and transformation
masterTo perform custom logic or validation during conversion, implement these two instance methods:
- (BOOL)modelCustomTransformFromDictionary:(NSDictionary *)dic: Called after JSON is converted to a model. ReturnYESif valid, orNOto ignore the model. Use this for manual type conversion (e.g., timestamp toNSDate).- (BOOL)modelCustomTransformToDictionary:(NSMutableDictionary *)dic: Called before the model is converted to JSON. ReturnYESif valid, orNOto ignore the conversion.
@implementation User - (BOOL)modelCustomTransformFromDictionary:(NSDictionary *)dic { // Custom logic here return YES; } - (BOOL)modelCustomTransformToDictionary:(NSMutableDictionary *)dic { // Custom logic here return YES; } @endImplement Coding, Copying, Hashing, Equality, and Description
masterYYModel provides helper methods to simplify the implementation of standard Objective-C protocols. Instead of manually mapping every property, call the
yy_modelprefixed methods within the standard protocol methods.@implementation YYShadow - (void)encodeWithCoder:(NSCoder *)aCoder { [self yy_modelEncodeWithCoder:aCoder]; } - (id)initWithCoder:(NSCoder *)aDecoder { self = [super init]; return [self yy_modelInitWithCoder:aDecoder]; } - (id)copyWithZone:(NSZone *)zone { return [self yy_modelCopy]; } - (NSUInteger)hash { return [self yy_modelHash]; } - (BOOL)isEqual:(id)object { return [self yy_modelIsEqual:object]; } - (NSString *)description { return [self yy_modelDescription]; } @endConfigure container properties (Arrays, Sets, Dictionaries)
masterFor container properties like
NSArray,NSSet, orNSMutableDictionary, you must specify the type of the elements contained within them by implementing the+ (NSDictionary *)modelContainerPropertyGenericClassclass method. The value in the returned dictionary should be theClassor the class name as a string.```objc @implementation Attributes + (NSDictionary *)modelContainerPropertyGenericClass { return @{@Use Whitelists and Blacklists for properties
masterYou can control which properties are included or excluded during conversion by implementing the following class methods:
+ (NSArray *)modelPropertyBlacklist: Properties in this list will be ignored.+ (NSArray *)modelPropertyWhitelist: Only properties in this list will be processed.
```objc @implementation Attributes + (NSArray *)modelPropertyBlacklist { return @[@"test1", @Map model properties to different JSON keys
masterIf your model property names do not match the JSON keys, implement the
+ (NSDictionary *)modelCustomPropertyMapperclass method. You can map a property to a single key path string or an array of key paths.- JSON to Model: If a property maps to multiple keys, YYModel uses the first non-null value found in the order provided.
- Model to JSON: If a property maps to multiple keys, YYModel only processes the first key path. If multiple properties map to the same key, it uses any non-null value.
```objc @implementation Book + (NSDictionary *)modelCustomPropertyMapper { return @{@Convert JSON to Model and vice versa
masterUse
yy_modelWithJSON:to convert JSON (NSData, NSString, or NSDictionary) into a model instance. Useyy_modelToJSONObjectto convert a model instance back into a JSON object (NSDictionary).// Convert json to model: User *user = [User yy_modelWithJSON:json]; // Convert model to json: NSDictionary *json = [user yy_modelToJSONObject];