生活随笔
收集整理的這篇文章主要介紹了
IOS15之JSON的解析字典转模型
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
IOS15之JSON的解析字典轉(zhuǎn)模型
IOS15字典轉(zhuǎn)模型 基于 iPhone13運(yùn)行的。
涉及KVC,
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface AHLJProduct
: NSObject
@property(nonatomic
,copy
)NSString
* title
;
@property(nonatomic
,copy
)NSString
* ids
;
@property(nonatomic
,copy
)NSString
* url
;
@property(nonatomic
,copy
)NSString
* icon
;
@property(nonatomic
,copy
)NSString
* customUrl
;
-(instancetype
)initWithDict
:(NSDictionary
*)dict
;
+(instancetype
)productWithDict
:(NSDictionary
*)dict
;
@end
NS_ASSUME_NONNULL_END
#import "AHLJProduct.h"
@implementation AHLJProduct
- (instancetype
)initWithDict
:(NSDictionary
*)dict
{self = [super init
];if (self) {[self setValuesForKeysWithDictionary
:dict
];self.ids
= dict
[@"id"];}return self;
}
+ (instancetype
)productWithDict
:(NSDictionary
*)dict
{return [[self alloc
] initWithDict
:dict
];
}
- (void)setValue
:(id
)value forUndefinedKey
:(NSString
*)key
{}
- (NSString
*)description
{return [NSString stringWithFormat
:@"< %p %@, {title: %@,url: %@, id,%@,customUrl %@,icon: %@}>", self, [self class
],self.title
,self.url
,self.ids
,self.customUrl
,self.icon
];
}
@end
- (NSArray
*)products
{if(!_products
){NSString
*path
= [[NSBundle mainBundle
] pathForResource
:@"more_project.json" ofType
:nil
];NSData
*data
= [NSData dataWithContentsOfFile
:path
];NSArray
*dataArray
= [NSJSONSerialization JSONObjectWithData
:data options
:NSJSONReadingMutableContainers error
:nil
];NSMutableArray
*mArr
= [NSMutableArray array
];for(NSDictionary
*dict
in dataArray
){[mArr addObject
:[AHLJProduct productWithDict
:dict
]];}_products
= mArr
;}return _products
;
}
項(xiàng)目地址:
https://gitee.com/johnson__save_admin/iosjsonparse-demo.git
總結(jié)
以上是生活随笔為你收集整理的IOS15之JSON的解析字典转模型的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。