前言:
本篇介绍一下Sagit框架中的模型基类STModelBase、和STModel常见用法。
STModelBase:所有实体的基类,自己定义的实体继承自此基类。
STModel:内部预先实现的给STHttp网络请求返回的结果使用的实体类。
1、框架中的Model相关实体介绍:
STModel 已更名:STHttpModel
下面分别介绍:
A:STEnum:定义了框架中用到的枚举类型:
typedef NS_ENUM(NSUInteger,RootViewControllerType) {
RootViewDefaultType,
RootViewNavigationType,
RootViewTabBarType
};
typedef NS_ENUM(NSUInteger,XYFlag) {
XY=0,
X=1,
Y=2,
};
//!布局时的相对位置(取值的依据为:Left:1 Top:2 Ritht:3 Bottom:4 可以根据值来检测所相对哪些位置)
typedef NS_ENUM(NSUInteger,XYLocation) {
Left = 1,
LeftTop = 12,
LeftTopRight = 123,
LeftTopBottom = 124,
LeftRight = 13,
LeftBottom = 14,
LeftBottomRight = 143,
Top = 2,
TopRight = 23,
TopBottom = 24,
TopRightBottom = 234,
Right = 3,
RightBottom = 34,
Bottom = 4,
//相对四边
LeftTopRightBottom = 1234
};
只有三个:
1、UIWindow的根视图类型:RootViewControllerType
2、布局时控制滑动的方法或方位:XYFlag。
3、布局时控制相对位置:XYLocation。
2、STLayoutTracer:框架内部使用,用于跟踪自动布局使用。
该实体会记录每一个UI界面的布局方式,核心用于布局刷新。
3、STHttpModel: STHttp网络请求默认返回的实体类型。
基本属性只有两个:返回成成功标识:success、可以接收任意类型参数的msg。
@interface STHttpModel : STModelBase
@property (nonatomic, assign) BOOL success;
@property (retain, nonatomic) id<NSObject> msg;
//将msg转成dictionary返回
@property (retain, nonatomic,readonly) NSDictionary* msgDic;
//将msg转成Array返回
@property (retain, nonatomic,readonly) NSArray* msgArray;
//将msg转成string返回
@property (copy, nonatomic,readonly) NSString* msgString;
@end
接收的Json结果参数为:{success:true,msg:xxxxx}
其它参数,msgDic、msgArray、msgString,是方面把msg进行类型转换返回数据。
用法示例1:结果转字典,再从字典里拿数据转数组。
用法示例2:结果转其它Model(自已的Model要继承自STModelBase)
4、STModelBase:所有Model可继承的基类。
基本定义:
@interface STModelBase:NSObject
-(id)init;
-(id)initWithObject:(id<NSObject>)msg;
-(id)initWithDictionary:(NSDictionary*)dic;
-(NSDictionary*)toDictionary;
//!指定的属性名称是否忽略。
-(BOOL)isIgnore:(NSString*)name;//转JSON。
-(NSString*)toJson;
//!NSArray<NSDictionary> 转 NSArray<Model>
+(NSArray<id>* )toArrayEntityFrom:(NSArray*)array;
@end
基本功能:
1、初始化功能:可以从字典转实体。
2、isIgnore方法:可以设置忽略不处理的字段属性。
3、toJson:可以转json。
4、toArrayEntityFrom:可以转NSArray<Model>。
复杂实体的定义示例:
@protocol PersonalUser
@end
@interface PersonalUser : STModelBase
@property (nonatomic, copy) NSString *RongYunToken;
@property (nonatomic, copy) NSString *UserID;
@property (nonatomic, copy) NSString *UserName;
@property (nonatomic, assign) NSInteger Edu;
@property (nonatomic, copy) NSString *EduText;
@property (nonatomic, assign) NSInteger Gender;
//@property (nonatomic, copy) NSString *GenderText;
@property (nonatomic, copy) NSString *Longitude;
@property (nonatomic, copy) NSString *NickName;
@property (nonatomic, assign) NSInteger AccountType;
//@property (nonatomic, copy) NSString *AccountTypeText;
@property (nonatomic, copy) NSString *Company;
@property (nonatomic, assign) NSInteger Wages;
@property (nonatomic, copy) NSString *WagesText;
@property (nonatomic, copy) NSString *GPSAddress;
@property (nonatomic, copy) NSString *Description;
@property (nonatomic, assign) NSInteger MarrStatus;
@property (nonatomic, copy) NSString *MarrStatusText;
@property (nonatomic, copy) NSString *Latitude;
@property (nonatomic, assign) NSInteger IsReal;
@property (nonatomic, assign) NSInteger Age;
@property (nonatomic, copy) NSString *AgeText;
@property (nonatomic, assign) NSInteger Profession;
@property (nonatomic, copy) NSString *ProfessionText;
//头像的路径
@property (nonatomic,copy) NSString *PhotoPath;
//城市
@property (nonatomic, copy) NSString *City;
//积分
@property (nonatomic, assign) NSInteger Integral;
//星座
@property (nonatomic, assign) NSInteger Constellation;
@property (nonatomic, copy) NSString *ConstellationText;
//身高
@property (nonatomic, assign) NSInteger Height;
@property (nonatomic, copy) NSString *HeightText;
//距离
@property (nonatomic, assign) NSInteger Distance;
//Poker 花色
@property (nonatomic, assign) NSInteger PokerSuit;
//Poker 数字
@property (nonatomic, assign) NSInteger PokerRank;
@end
@protocol PersonalPhoto
@end
@interface PersonalPhoto : STModelBase
@property (nonatomic, copy) NSString *FKID;//外键,比如存的朋友圈的TopicID
@property (nonatomic, copy) NSString *PhotoID;
@property (nonatomic, copy) NSString *PhotoPath;
@property (nonatomic, copy) NSString *BigPhotoPath;
@property (nonatomic, assign) NSInteger PhotoType;
@property (nonatomic, assign) NSInteger ExaStatus;
@property (nonatomic,copy) NSString* ExaStatusText;
@end
@interface PersonalModel : STModelBase
@property (nonatomic, strong) PersonalUser<PersonalUser> *user;
@property (nonatomic, strong) NSMutableArray<PersonalPhoto> *photos;
@property (nonatomic, retain) PersonalPhoto *headPhoto;
@property (nonatomic, retain) NSMutableArray<PersonalPhoto> *bodyPhoto;
@end
控制字段忽略Json转换的只要重写方法:
本文到此结束。