ibireme / yymodel Goto Github PK
View Code? Open in Web Editor NEWHigh performance model framework for iOS/OSX.
License: MIT License
High performance model framework for iOS/OSX.
License: MIT License
之前看过大兄弟你博客上对几个主流转模型框架的评测,但是没有加入Swift的,所以不知道YYModel
的效率和SwiftyJSON
比如何?
Consider a situation like this:
NSDictionary *dict = @{@"name":@"Harry Potter",
@"authors":@[@{@"name":@"J.K."},
@{@"name":@"YY"}
]};
Book *book = [Book yy_modelWithDictionary:dict];
I have a book with 2 authors, and I expect it will give me 2 Author models by YYModel, however it will keep authors as original NSDictionay array, even if I declare author as NSArray<Author *> *author, could you give some advice?
HI ibireme~
YYClassPropertyInfo 的 initWithProperty:方法中
即YYClassInfo.m 的第 172行的代码
if (type & YYEncodingTypeObject)
不是很理解
这段的判断我的理解是 type是否为 YYEncodingTypeObject (可能是我理解错了?)
但是type的枚举是 0 ~ 22
那么YYEncodingTypeObject 为 14 即 0000 1110
和它进行 & 运算 的话 只要有 8 4 2组成的类型(如:YYEncodingTypeUInt64 = 10,YYEncodingTypePointer = 18)都会进入if的分支,但这又违背了这一行if的初衷
所以希望能解答一下
When I first start to use YYModel, I made a model like this to match the corresponding JSON:
@interface WeatherDetail : NSObject
@property (nonatomic) NSNumber *id;
@property (nonatomic) NSString *main;
@property (nonatomic) NSString *description;
@end
Apparently the name id
and description
are ambiguous and may cause confusion.
After some research I found out that I could use custom mapping to avoid this:
+ (NSDictionary *)modelCustomPropertyMapper {
return @{@"weatherId": @"id",
@"weatherDescription": @"description"
};
}
Considering that YYModel add extension directly to NSObject
, it may be better if YYModel could warn users when they're trying to use reserved words and ivars.
最近在做一个项目,要在UI层做配置,希望支持json配置。由于UI层的属性会有UIColor等YYModel原生未支持的属性转换,现在需要在 modelCustomTransformFromDictionary 中自个儿定义,如果能在这个方法中获得属性的类型,那就一个for循环就可以把所有UIColor类型属性自动转换掉,不需要去按照属性名判断。
另外还涉及到从NSNumber转换到 NSDate,自定义NSBlock。如果这里能获得属性的 YYClassPropertyInfo 就很好处理了。
I have updated YYModel to latest version, when using yy_modelWithJSON the result is nil !
请问model to json和son to model的时候怎么设置不同配置的白黑名单?
有时项目中接口的情况可能会比较复杂。比方说一个项目中,在A接口中用户id叫userid,B接口中叫uid,C接口中叫id ......
可是modelCustomPropertyMapper方法只能实现一次,字典的一个键也只能对应一个值
这种情况下,假如都想使用一个叫YYUser的model中的userid属性的话,该肿么办?
(粗想似乎有两种解决方案,一种是modelCustomPropertyMapper返回的字典中,userid键对应的可以是一个数组或是集合;另一种是将该字典中键和值的位置交换一下)
如果能支持泛型就不需要这个方法了 + modelContainerPropertyGenericClass
@interface Attributes
@property NSString *name;
@property NSArray<Shadow *> *shadows;
@property NSSet<Border *> *borders;
@property NSMutableDictionary<NSString *, Attachment *> *attachments;
@end
现在出现了一种情况,就是我们的数据模型是多层的,有些属性是定义在父类中的,父类中的某些属性是容器类型,其中成员的类型由父类的modelContainerPropertyGenericClass方法定义。
但现在又有子类继承了这个父类,其中也实现了modelContainerPropertyGenericClass方法,来制定他自己的容器类型的属性的成员class,但这就把父类的实现覆盖了。
现在除了在子类中把父类的mapping加进来之外,有没有更好的方法?比如在生成YYModelMeta的时候,自动递归处理父类的这些自定义的mapping?因为既然已经自动递归处理了父类的属性,那么自动处理父类的各种自定义mapping也是很必要的
这是我的一个model:
@interface RecommendCellLayout : NSObject <NSCoding>
@property (nonatomic, strong) Topic *topic;
@property (nonatomic, strong) AD *ad;
@property (nonatomic, assign) BOOL isGif;
@property (nonatomic, assign) int imgHeight; //这个也出现过
@property (nonatomic, assign) int height; //出现问题最多的就是这个,正确的值一般为194
@end
实现文件里加入了以下代码
- (void)encodeWithCoder:(NSCoder *)aCoder { [self modelEncodeWithCoder:aCoder]; }
- (id)initWithCoder:(NSCoder *)aDecoder { return [self modelInitWithCoder:aDecoder]; }
我会直接将这个model所在的数组存储到本地,用过Yapdatabase,也用过YYDiskCache,最后读取出来的height值都为正或者负的2147483648(数字可能有变化);然而我打断点看到同一个对象中setter方法中传进来的就是194,getter方法中获取的_height却为-2147483648。。百思不得其解,后来我导入了MJExtension,将encodeWithCoder和initWithCoder这两个方法注释,换成了MJCodingImplementation这个宏,再次编译程序,decode出来的int height就都是正确的值了。。。希望YY神解决后可以大致告知下。
测试情况: 4s的8.4和9.2有这个问题 5s的8.4和9.2没问题
将height的名字改成cellHeight 5s的8.4和9.2有这个问题 4s的8.4和9.2没问题
6p的9.2全部没问题
@class YYMessageInfo;
@interface YYMessage : NSObject
@property (nonatomic, assign) uint64_t messageId;
@property (nonatomic, strong) NSString *content;
@property (nonatomic, strong) NSDate *time;
@property (nonatomic, strong) YYMessageInfo *info;
@end
@interface YYMessageInfo : NSObject
@property (nonatomic, strong) NSString *date;
@property (nonatomic, strong) NSString *name;
@end
@implementation YYMessage
+(NSDictionary *)modelCustomPropertyMapper {
return @{@"messageId":@"i",
@"content":@"c",
@"time":@"t",
@"info.date":@"date",
@"info.name":@"name"};
}
- (void)modelCustomTransformFromDictionary:(NSDictionary *)dic {
uint64_t timestamp = [dic unsignedLongLongValueForKey:@"t" default:0];
self.time = [NSDate dateWithTimeIntervalSince1970:timestamp / 1000.0];
}
- (void)modelCustomTransformToDictionary:(NSMutableDictionary *)dic {
dic[@"t"] = @([self.time timeIntervalSince1970] * 1000).description;
}
@end
static void CustomMapperExample() {
YYMessage *message = [YYMessage modelWithJSON:@"{\"i\":\"2000000001\",\"c\":\"Hello\",\"t\":\"1437237598000\",\"date\":\"now\",\"name\":\"QingDou\"}"];
NSString *messageJSON = [message modelToJSONString];
NSLog(@"%s -- Book: %@",__func__, messageJSON);
}
代码如上,可否添加关于这种情况下的自定义解析规则。
我用YYMODEL。经常用于服务端返回数据。
如果返回数据是一个列表。。列表对应的是一个Model。
如何直接返回responseObject转换成数组呢
NSMutableArray <LVProducts *>_productsArray =
我知道如果在定义一层 LVProductsListModel 包含NSMutableArray <LVProducts *>_productsArray 属性可以。但是这样很多余。所以问问有没很直接的方法
@implementation ItemValue
+ (NSDictionary *)modelCustomPropertyMapper {
return @{@"district_id" : @"districtId",
@"bizcircle_id" : @"bizcircleId",
@"subway_line_id" : @"subwayLineId",
@"subway_station_id" : @"subwayStationId"};
}
@end
NSDictionary *dictionary = [item.value yy_modelToJSONObject];
使用了 mapper怎样才能让返回的字典 @{@"distric_id": sh1xxxxxx} 而不是@{@"districId": sh1xxxxxx}
存入UIColor属性。然后modelToJSONString,会崩溃。
对象有一个nsaary 容器里面是自定义对象(该自定义对象里面还有一个容器)json解析都没问题,yy_modelCopy的时候容器copy 不成功。
首先对大神进行膜拜并表示敬意...
第一种情况如果一个模型包含另一个模型,另一个模型里包含数组 解析后如何访问?
{
"items": [
{
"title": "首页",
"key": "tab_home",
"subitems": [
{
"type": "tab",
"title": "首页",
"image": "[email protected]",
"image_hl": "[email protected]"
}
]
},
{
"title": "直播",
"key": "tab_live",
"subitems": [
{
"type": "tab",
"title": "直播",
"image": "[email protected]",
"image_hl": "[email protected]"
}
]
},
第二种情况模型中包含模型,为什么不能用model.model.属性的方法访问...
{
"items": [
{
"title": "首页",
"key": "tab_home",
"subitems": {
"type": "tab",
"title": "首页",
"image": "[email protected]",
"image_hl": "[email protected]"
}
},
{
"title": "直播",
"key": "tab_live",
"subitems":
{
"type": "tab",
"title": "直播",
"image": "[email protected]",
"image_hl": "[email protected]"
}
},
xcode报错说我第二个模型是一个字典...
字典包数组包字典 中最下级的字典中key修改不成功.
是要�把结果转换成json然后转换成model吗? 但是这样的感觉并不好,我也没有什么好主意。
Hi,
First of all I'd like to thank you for this awesome library.
Secondly, I would like to mention that the library does not currently support special characters.
For example, if I have é
or è
or à
or any special (french) character in the json, I have a null NSString
property.
Can you please verify it, it's a very important feature.
Thank you very much.
现在在不支持 yyyy/MM/dd HH:mm:ss 格式的日期的情况下,会使用yyyy-MM-dd HH:mm:ss进行转换,但是转换出的结果,是不正确的日期。
对于不支持的日期格式,应该不转化比较好。 转换出一个错误的日期,容易让人误解。
上面使用的数据:"createDate":"2015/11/25 16:05:29" ,默认 好像会转成 2014 年12月30日00:00:00
当时MJ也做过性能测试,远远比其他用property的性能高, 后来MJExtension要支持core data,也用了property,但是 ,YYModel的写法要底层一点,和MJExtension比得话,用他的0.0.3版
如题,对应的代码如下,dic.count 为0 时是正确的
case YYEncodingTypeNSDictionary:
case YYEncodingTypeNSMutableDictionary: {
NSDictionary *dic = (id)model;
NSMutableString *desc = [NSMutableString new];
if (dic.count == 0) {
return [desc stringByAppendingString:@"{}"];
} else {
NSArray *keys = dic.allKeys;
[desc appendFormat:@"[\n"];
for (NSUInteger i = 0, max = keys.count; i < max; i++) {
NSString *key = keys[i];
NSObject *value = dic[key];
[desc appendString:@" "];
[desc appendFormat:@"%@ = %@",key, ModelDescriptionAddIndent(ModelDescription(value).mutableCopy, 1)];
[desc appendString:(i + 1 == max) ? @"\n" : @";\n"];
}
[desc appendString:@"]"];
}
return desc;
}
从jsonmodel转来,使用yymodel的对象通过NSLog打印的话 并不能明确看到其各项属性的内容.继承于jsonmodel对象可以打印(因为description).还是我没有用对yymodel方法??有时候 NSLog对于我来说很有必要,不必通过转换字典来打印.
如果服务器给的json有的字段是NULL或者没有这个字段的时候,如果是字符串的话就返回@“”,数字的话就0 或者1 ,一个默认的值多好,因为客户端拿到这个值以后还的去判断是否为空。
RT
RT,提示如下
[!] The platform of the target Pods
(OS X 10.11) is not compatible with YYModel (0.9.8)
, which does not support osx
.
@property id data;
这种未标明类型的属性是否可以不转换,直接把值赋给data。 现在的处理方式是直接把值忽略了。
I'm using YYModel with Swift, then I encountered an issue that I cannot match model property to different JSON key. Method modelCustomPropertyMapper
seems does not work.
my model object:
class User: NSObject {
var name = ""
var createDate = NSDate()
var aBool = true
var aDouble = 0.0
var anInt = 1
var stringArray = [String]()
var aSubModel = SubModel()
func modelCustomPropertyMapper() -> NSDictionary {
return ["anInt": "exactlyInt"]
}
}
And, the date property converted from ISO8601 date string such as 2015-11-10T00:00:00.000Z
in JSON cannot be accessed. It raised an EXC_BAD_ACCESS
error.
Date string formatted in Wed Sep 16 12:20:09 +0800 2015
style looks worked fine.
Hi ibireme, thank you for bring us this great library.
My concern is that OSSpinLock is unsafe in iOS. It does not guarantee progress in the face of priority inversion.
http://mjtsai.com/blog/2015/12/16/osspinlock-is-unsafe/
https://lists.swift.org/pipermail/swift-dev/Week-of-Mon-20151214/000372.html
2015-12-15 14:15:24.000 这种时间格式解析返回nil
the swift class or struct can use this lib. thanks .
yymodel真的如在网上看到的比mantle效率高吗
Please add change log so that everybody would know what the version update really means.
NSTaggedPointerString 并不能进行判空处理,然而我的服务器大哥经常给我传过来一个 NSNull....
How can I do......
Currently YYModel use +new method to init instance, but in some cases, user may need their custom initialization method, especially use YYModel with other local database model.
这个没法自动提示么?请求大神帮忙,太喜欢你的框架了
Met this warning when use 'pod search YYModel' command.
当使用 YY_copy 拷贝模型的时候,可以拷贝自己,但是对该模型中的子模型没有生成新拷贝,只是将子模型的地址进行赋值。
有没有提供方法或者可以调用的位置让模型递归的拷贝子模型? (本来想在 copyWithZone 里面对子模型进行拷贝的,但在运行过程中程序不会跑到这里来)
YY_isEqual 方法是否也支持多级的模型对比呢?
please show me the image
想咨询一下:
YYClassInfo 类中, superCls 属性为什么要声明为strong呢?
当服务器在相关字段返回了空值的时候,客户端需要设置一个默认值。
rt,求解,谢谢!
//JSON
{"provinces":[...]}
//Model
class SWCity:NSObject{
var ID:String? = ""
var name:String? = ""
}
class SWProvince:NSObject{
var ID:String? = ""
var name:String? = ""
var cities:[SWCity] = []
}
class SWCountry:NSObject{
var provinces:[SWProvince] = []
}
//报错
fatal error: NSArray element failed to match the Swift Array Element type
if let country:SWCountry = SWCountry.yy_modelWithJSON(dic)
{
for province in country.provinces
{
print(province)
}
}
写这些方法时没有自动完成(不知是否还有其它类似方法):
+ (NSDictionary *)modelCustomPropertyMapper;
+ (NSDictionary *)modelContainerPropertyGenericClass;
- (BOOL)modelCustomTransformFromDictionary:(NSDictionary *)dictionary;
建议在 NSObject+YYModel
里加入相关方法的声明和缺省实现:
+ (NSDictionary *)yy_modelCustomPropertyMapper;
+ (NSDictionary *)yy_modelContainerPropertyGenericClass;
- (BOOL)yy_modelCustomTransformFromDictionary:(NSDictionary *)dictionary;
我碰到一个attr的字典,字段是中文的,还遇到一个装图片的数组,里面的字段也是没名字的,value1,2,3,...
YYModel,有相关的解决方法吗,那个容器类的,我看了一下,感觉是要建个模型才能实现吧,而我这个有点特殊,不知道该怎么解析啊,求YY大神指点...
{
code = 0;
user = {
avatar = "<null>";
description = "<null>";
"wechat_id" = "<null>";
"weibo_id" = "<null>";
};
}
有个疑问。从服务端返回的数据得到的字符串json包含有nil的。
在解析的过程中。获取得到对象会有nil的情况。然后程序中使用到这个对象的属性就会carsh。
那么我觉得不应该成属性地方逐一判断。想在入口处处理这个问题。
那么我有个疑问就是。YY大神怎么考虑这个问题的。是否有控制忽略字符串为nil转换@”“的属性开关呢。还是故意就这样让使用YYModel的人自己去处理呢。
虽然我已经从你源码处解决这个问题。但还是想和你确认一下。交流一下你的心得。我已经提过很多问题了。还是再次谢谢你
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.