YYModel Documentation

repository·master·Indexed 26 days ago

https://github.com/ibireme/yymodel

A 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.

Tokens
1.2K
Snippets
6
Records
9
Agent score
38%

What's inside YYModel

  1. Handle nested models

    master
    YYModel 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.
  2. Implement custom data validation and transformation

    master

    To perform custom logic or validation during conversion, implement these two instance methods:

    • - (BOOL)modelCustomTransformFromDictionary:(NSDictionary *)dic: Called after JSON is converted to a model. Return YES if valid, or NO to ignore the model. Use this for manual type conversion (e.g., timestamp to NSDate).
    • - (BOOL)modelCustomTransformToDictionary:(NSMutableDictionary *)dic: Called before the model is converted to JSON. Return YES if valid, or NO to ignore the conversion.
    @implementation User
    - (BOOL)modelCustomTransformFromDictionary:(NSDictionary *)dic {
        // Custom logic here
        return YES;
    }
    
    - (BOOL)modelCustomTransformToDictionary:(NSMutableDictionary *)dic {
        // Custom logic here
        return YES;
    }
    @end
  3. Implement Coding, Copying, Hashing, Equality, and Description

    master

    YYModel provides helper methods to simplify the implementation of standard Objective-C protocols. Instead of manually mapping every property, call the yy_model prefixed 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]; }
    @end
  4. Configure container properties (Arrays, Sets, Dictionaries)

    master

    For container properties like NSArray, NSSet, or NSMutableDictionary, you must specify the type of the elements contained within them by implementing the + (NSDictionary *)modelContainerPropertyGenericClass class method. The value in the returned dictionary should be the Class or the class name as a string.

    ```objc
    @implementation Attributes
    + (NSDictionary *)modelContainerPropertyGenericClass {
        return @{@
  5. Use Whitelists and Blacklists for properties

    master

    You 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", @
  6. Map model properties to different JSON keys

    master

    If your model property names do not match the JSON keys, implement the + (NSDictionary *)modelCustomPropertyMapper class 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 @{@
  7. Convert JSON to Model and vice versa

    master

    Use yy_modelWithJSON: to convert JSON (NSData, NSString, or NSDictionary) into a model instance. Use yy_modelToJSONObject to 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];