| Number of watchers on Github | 3382 |
| Number of open issues | 104 |
| Average time to close an issue | 8 days |
| Main language | Objective-C |
| Average time to merge a PR | 1 day |
| Open pull requests | 20+ |
| Closed pull requests | 14+ |
| Last commit | about 2 years ago |
| Repo Created | almost 4 years ago |
| Repo Last Updated | over 1 year ago |
| Size | 920 KB |
| Organization / Author | ibireme |
| Latest Release | 1.0.4 |
| Contributors | 6 |
| Page Updated | 2018-03-12 |
| Do you use YYModel? Leave a review! | |
| View open issues (104) | |
| View YYModel activity | |
| View on github | |
| Fresh, new opensource launches 🚀🚀🚀 | |
|
Trendy new open source projects in your inbox!
View examples
|
|
High performance model framework for iOS/OSX.
(It's a component of YYKit)
Time cost (process GithubUser 10000 times on iPhone 6):

See Benchmark/ModelBenchmark.xcodeproj for more benchmark case.
// JSON:
{
"uid":123456,
"name":"Harry",
"created":"1965-07-31T00:00:00+0000"
}
// Model:
@interface User : NSObject
@property UInt64 uid;
@property NSString *name;
@property NSDate *created;
@end
@implementation User
@end
// Convert json to model:
User *user = [User yy_modelWithJSON:json];
// Convert model to json:
NSDictionary *json = [user yy_modelToJSONObject];
If the type of an object in JSON/Dictionary cannot be matched to the property of the model, the following automatic conversion is performed. If the automatic conversion failed, the value will be ignored.
| JSON/Dictionary | Model |
|---|---|
| NSString | NSNumber,NSURL,SEL,Class |
| NSNumber | NSString |
| NSString/NSNumber | C number (BOOL,int,float,NSUInteger,UInt64,...) NaN and Inf will be ignored |
| NSString | NSDate parsed with these formats: yyyy-MM-dd yyyy-MM-dd HH:mm:ss yyyy-MM-dd'T'HH:mm:ss yyyy-MM-dd'T'HH:mm:ssZ EEE MMM dd HH:mm:ss Z yyyy |
| NSDate | NSString formatted with ISO8601: "YYYY-MM-dd'T'HH:mm:ssZ" |
| NSValue | struct (CGRect,CGSize,...) |
| NSNull | nil,0 |
| "no","false",... | @(NO),0 |
| "yes","true",... | @(YES),1 |
// JSON:
{
"n":"Harry Pottery",
"p": 256,
"ext" : {
"desc" : "A book written by J.K.Rowing."
},
"ID" : 100010
}
// Model:
@interface Book : NSObject
@property NSString *name;
@property NSInteger page;
@property NSString *desc;
@property NSString *bookID;
@end
@implementation Book
+ (NSDictionary *)modelCustomPropertyMapper {
return @{@"name" : @"n",
@"page" : @"p",
@"desc" : @"ext.desc",
@"bookID" : @[@"id",@"ID",@"book_id"]};
}
@end
You can map a json key (key path) or an array of json key (key path) to one or multiple property name. If there's no mapper for a property, it will use the property's name as default.
// JSON
{
"author":{
"name":"J.K.Rowling",
"birthday":"1965-07-31T00:00:00+0000"
},
"name":"Harry Potter",
"pages":256
}
// Model: (no need to do anything)
@interface Author : NSObject
@property NSString *name;
@property NSDate *birthday;
@end
@implementation Author
@end
@interface Book : NSObject
@property NSString *name;
@property NSUInteger pages;
@property Author *author;
@end
@implementation Book
@end
@class Shadow, Border, Attachment;
@interface Attributes
@property NSString *name;
@property NSArray *shadows; //Array<Shadow>
@property NSSet *borders; //Set<Border>
@property NSMutableDictionary *attachments; //Dict<NSString,Attachment>
@end
@implementation Attributes
+ (NSDictionary *)modelContainerPropertyGenericClass {
// value should be Class or Class name.
return @{@"shadows" : [Shadow class],
@"borders" : Border.class,
@"attachments" : @"Attachment" };
}
@end
@interface User
@property NSString *name;
@property NSUInteger age;
@end
@implementation Attributes
+ (NSArray *)modelPropertyBlacklist {
return @[@"test1", @"test2"];
}
+ (NSArray *)modelPropertyWhitelist {
return @[@"name"];
}
@end
// JSON:
{
"name":"Harry",
"timestamp" : 1445534567
}
// Model:
@interface User
@property NSString *name;
@property NSDate *createdAt;
@end
@implementation User
- (BOOL)modelCustomTransformFromDictionary:(NSDictionary *)dic {
NSNumber *timestamp = dic[@"timestamp"];
if (![timestamp isKindOfClass:[NSNumber class]]) return NO;
_createdAt = [NSDate dateWithTimeIntervalSince1970:timestamp.floatValue];
return YES;
}
- (BOOL)modelCustomTransformToDictionary:(NSMutableDictionary *)dic {
if (!_createdAt) return NO;
dic[@"timestamp"] = @(n.timeIntervalSince1970);
return YES;
}
@end
@interface YYShadow :NSObject <NSCoding, NSCopying>
@property (nonatomic, copy) NSString *name;
@property (nonatomic, assign) CGSize size;
@end
@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
pod 'YYModel' to your Podfile.pod install or pod update.github "ibireme/YYModel" to your Cartfile.carthage update --platform ios and add the framework to your project.YYModel.h.Full API documentation is available on CocoaDocs.
You can also install documentation locally using appledoc.
This library requires iOS 6.0+ and Xcode 8.0+.
YYModel is provided under the MIT license. See LICENSE file for details.
==============
iOS/OSX
( YYKit )
============== GithubUser 10000 (iPhone 6):

Benchmark/ModelBenchmark.xcodeproj
==============
==============
// JSON:
{
"uid":123456,
"name":"Harry",
"created":"1965-07-31T00:00:00+0000"
}
// Model:
@interface User : NSObject
@property UInt64 uid;
@property NSString *name;
@property NSDate *created;
@end
@implementation User
@end
// JSON (NSData,NSString,NSDictionary) Model:
User *user = [User yy_modelWithJSON:json];
// Model JSON :
NSDictionary *json = [user yy_modelToJSONObject];
JSON/Dictionary Model YYModel
| JSON/Dictionary | Model |
|---|---|
| NSString | NSNumber,NSURL,SEL,Class |
| NSNumber | NSString |
| NSString/NSNumber | (BOOL,int,float,NSUInteger,UInt64,...) NaN Inf |
| NSString | NSDate : yyyy-MM-dd yyyy-MM-dd HH:mm:ss yyyy-MM-dd'T'HH:mm:ss yyyy-MM-dd'T'HH:mm:ssZ EEE MMM dd HH:mm:ss Z yyyy |
| NSDate | NSString ISO8601: "YYYY-MM-dd'T'HH:mm:ssZ" |
| NSValue | struct (CGRect,CGSize,...) |
| NSNull | nil,0 |
| "no","false",... | @(NO),0 |
| "yes","true",... | @(YES),1 |
// JSON:
{
"n":"Harry Pottery",
"p": 256,
"ext" : {
"desc" : "A book written by J.K.Rowing."
},
"ID" : 100010
}
// Model:
@interface Book : NSObject
@property NSString *name;
@property NSInteger page;
@property NSString *desc;
@property NSString *bookID;
@end
@implementation Book
// Dict Model JSON Key
+ (NSDictionary *)modelCustomPropertyMapper {
return @{@"name" : @"n",
@"page" : @"p",
@"desc" : @"ext.desc",
@"bookID" : @[@"id",@"ID",@"book_id"]};
}
@end
json key (key path)
json->model json key
model->json json key (key path) json key (key path) json key
// JSON
{
"author":{
"name":"J.K.Rowling",
"birthday":"1965-07-31T00:00:00+0000"
},
"name":"Harry Potter",
"pages":256
}
// Model:
@interface Author : NSObject
@property NSString *name;
@property NSDate *birthday;
@end
@implementation Author
@end
@interface Book : NSObject
@property NSString *name;
@property NSUInteger pages;
@property Author *author; //Book Author
@end
@implementation Book
@end
@class Shadow, Border, Attachment;
@interface Attributes
@property NSString *name;
@property NSArray *shadows; //Array<Shadow>
@property NSSet *borders; //Set<Border>
@property NSMutableDictionary *attachments; //Dict<NSString,Attachment>
@end
@implementation Attributes
// ( Class Class Name )
+ (NSDictionary *)modelContainerPropertyGenericClass {
return @{@"shadows" : [Shadow class],
@"borders" : Border.class,
@"attachments" : @"Attachment" };
}
@end
@interface User
@property NSString *name;
@property NSUInteger age;
@end
@implementation Attributes
//
+ (NSArray *)modelPropertyBlacklist {
return @[@"test1", @"test2"];
}
//
+ (NSArray *)modelPropertyWhitelist {
return @[@"name"];
}
@end
// JSON:
{
"name":"Harry",
"timestamp" : 1445534567
}
// Model:
@interface User
@property NSString *name;
@property NSDate *createdAt;
@end
@implementation User
// JSON Model
// NO Model
//
- (BOOL)modelCustomTransformFromDictionary:(NSDictionary *)dic {
NSNumber *timestamp = dic[@"timestamp"];
if (![timestamp isKindOfClass:[NSNumber class]]) return NO;
_createdAt = [NSDate dateWithTimeIntervalSince1970:timestamp.floatValue];
return YES;
}
// Model JSON
// NO Model
//
- (BOOL)modelCustomTransformToDictionary:(NSMutableDictionary *)dic {
if (!_createdAt) return NO;
dic[@"timestamp"] = @(n.timeIntervalSince1970);
return YES;
}
@end
@interface YYShadow :NSObject <NSCoding, NSCopying>
@property (nonatomic, copy) NSString *name;
@property (nonatomic, assign) CGSize size;
@end
@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
==============
pod 'YYModel'
pod install pod update
github "ibireme/YYModel"
carthage update --platform ios framework YYModel.h
============== CocoaDocs API appledoc
==============
iOS 6.0 Xcode 8.0
============== YYModel MIT LICENSE
==============
.ext
a...b) in some situation: #112