Code Monkey home page Code Monkey logo

yymodel's People

Contributors

andriitishchenko avatar arronzhang avatar brok3nhalo avatar chinsyo avatar digal avatar ibireme avatar iflyingboots avatar ivanvoile avatar iwill avatar leavesster avatar mattrobmattrob avatar skyline75489 avatar wenchaod avatar xinzhengzhang avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

yymodel's Issues

容器类属性有关的问题

我们这后台 如果返回的是一条数据的时候 返回的就是字典,如果有好几条的话 就是个数组,这样你使用NSArray *modelAr'r = [NSArray yy_modelArrayWithClass:[HometestM class ] json:nsarr]; 会有问题 。 当容器类属性不确定,+ (nullable NSDictionary<NSString *, id> *)modelContainerPropertyGenericClass
{

//这你怎么设置呢?
return @{@"pageAreaXmls" : [HomeTopM class],
};
default

}

在Swift环境下的效率如何?

之前看过大兄弟你博客上对几个主流转模型框架的评测,但是没有加入Swift的,所以不知道YYModel的效率和SwiftyJSON比如何?

How to support nested NSArray model in YYModel?

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?

YYClassPropertyInfo的一个小疑问

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的初衷
所以希望能解答一下

Warn users when they're trying to use ambiguous property names

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 就很好处理了。

yy_modelWithJSON nil

I have updated YYModel to latest version, when using yy_modelWithJSON the result is nil !

关于改进modelCustomPropertyMapper的一个想法

有时项目中接口的情况可能会比较复杂。比方说一个项目中,在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方法定义。
但现在又有子类继承了这个父类,其中也实现了modelContainerPropertyGenericClass方法,来制定他自己的容器类型的属性的成员class,但这就把父类的实现覆盖了。

现在除了在子类中把父类的mapping加进来之外,有没有更好的方法?比如在生成YYModelMeta的时候,自动递归处理父类的这些自定义的mapping?因为既然已经自动递归处理了父类的属性,那么自动处理父类的各种自定义mapping也是很必要的

关于decode时int出错,

这是我的一个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全部没问题

关于modelCustomPropertyMapper

@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 属性可以。但是这样很多余。所以问问有没很直接的方法

how to use mapped name by using modelToJasonObject

@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}

yy_modelCopy

对象有一个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修改不成功.

  • (NSDictionary *)modelCustomPropertyMapper {
    return @{@"itemName" : @"data.name"};
    }
    原key为name 上级是data(这个是数组) 单使用@"name"也不好用 ,
    当给方法的时候 原key值name会crash 应该是被改了

Special characters (é,è,à,...) in json

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 格式的日期的情况下,会使用yyyy-MM-dd HH:mm:ss进行转换,但是转换出的结果,是不正确的日期。
对于不支持的日期格式,应该不转化比较好。 转换出一个错误的日期,容易让人误解。
上面使用的数据:"createDate":"2015/11/25 16:05:29" ,默认 好像会转成 2014 年12月30日00:00:00

NSDictionary 的 description 返回格式为什么不是{} 而是[]啊

如题,对应的代码如下,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解析后的值能修改默认类型就好了

如果服务器给的json有的字段是NULL或者没有这个字段的时候,如果是字符串的话就返回@“”,数字的话就0 或者1 ,一个默认的值多好,因为客户端拿到这个值以后还的去判断是否为空。

处理`id`类型的属性转换

@property id data;

这种未标明类型的属性是否可以不转换,直接把值赋给data。 现在的处理方式是直接把值忽略了。

Issues when work with Swift

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.

ChangeLog

Please add change log so that everybody would know what the version update really means.

Custom initialization method

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.

YYModel 可以拷贝和对比嵌套的模型吗?

当使用 YY_copy 拷贝模型的时候,可以拷贝自己,但是对该模型中的子模型没有生成新拷贝,只是将子模型的地址进行赋值。

有没有提供方法或者可以调用的位置让模型递归的拷贝子模型? (本来想在 copyWithZone 里面对子模型进行拷贝的,但在运行过程中程序不会跑到这里来)

YY_isEqual 方法是否也支持多级的模型对比呢?

咨询一下

想咨询一下:
YYClassInfo 类中, superCls 属性为什么要声明为strong呢?

default value

当服务器在相关字段返回了空值的时候,客户端需要设置一个默认值。

对swift的基础数据类型支持还不够完善

//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;

解析对象属性为null

{
code = 0;
user = {

    avatar = "<null>";
    description = "<null>";

    "wechat_id" = "<null>";
    "weibo_id" = "<null>";
};

}

有个疑问。从服务端返回的数据得到的字符串json包含有nil的。
在解析的过程中。获取得到对象会有nil的情况。然后程序中使用到这个对象的属性就会carsh。
那么我觉得不应该成属性地方逐一判断。想在入口处处理这个问题。
那么我有个疑问就是。YY大神怎么考虑这个问题的。是否有控制忽略字符串为nil转换@”“的属性开关呢。还是故意就这样让使用YYModel的人自己去处理呢。
虽然我已经从你源码处解决这个问题。但还是想和你确认一下。交流一下你的心得。我已经提过很多问题了。还是再次谢谢你

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.