本身處理數據模型時,此種格式,無論屬性是否爲空,均可以任意使用,由於使用了排空方法,十分好用,強烈推薦,代碼以下:數組
模型.h文件中:atom
#import <Foundation/Foundation.h>url
@class User;spa
@interface Status : NSObject.net
@property (nonatomic,copy) NSString *created_at;orm
@property (nonatomic,assign) int64_t id;字符串
@property (nonatomic,copy) NSString *text;get
@property (nonatomic,copy) NSString *source;string
@property (nonatomic,strong) User *user;it
//配圖數組
@property (nonatomic,strong) NSArray *pic_urls;
@property (nonatomic,strong) Status * retweeted_status;
//配圖字符串數組
//@property (nonatomic,strong) NSMutableArray *pic_strs;
+ (instancetype)statusWithDict:(NSDictionary *)dict;
- (instancetype)initWithDict:(NSDictionary *)dict;
@end
模型.m文件中:
#import "Status.h"
#import "User.h"
@implementation Status
- (instancetype)initWithDict:(NSDictionary *)dict
{
self = [super init];
if (self) {
[self setValuesForKeysWithDictionary:dict];
// _ids = [NSString stringWithFormat:@"%@",dict[@"id"]];
// self.created_at = dict[@"created_at"];
// self.id = dict[@"id"];
// self.source = dict[@"source"];
// self.text = dict[@"text"];
// self.pic_urls = dict[@"pic_urls"];
// self.user = [User userWithDict:dict[@"user"]];
// self.retweeted_status = [Status statusWithDict:dict[@"retweeted_status"]];
//self.pic_strs = dict[@"pic_strs"];
}
return self;
}
+ (instancetype)statusWithDict:(NSDictionary *)dict{
return [[self alloc] initWithDict:dict];
}
- (void)setValue:(nullable id)value forKey:(nonnull NSString *)key{
if ([key isEqual: @"user"]) {
self.user = [User userWithDict:value];
return;
}
if ([key isEqualToString:@"retweeted_status"]) {
self.retweeted_status = [Status statusWithDict:value];
return;
}
[super setValue:value forKey:key];
}
//排空機制:當key value爲空時,程序不會崩潰
- (void)setValue:(nullable id)value forUndefinedKey:(nonnull NSString *)key{
}