其他分享
首页 > 其他分享> > Sagit.Framework For IOS 开发框架入门教程10:Model实体与网络请求返回数据实体基类转换。

Sagit.Framework For IOS 开发框架入门教程10:Model实体与网络请求返回数据实体基类转换。

作者:互联网

前言:

本篇介绍一下Sagit框架中的模型基类,和常见用法。

1、框架中的Model实体介绍:

 

下面分别介绍:

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、STModel: STHttp网络请求默认返回的实体类型。

基本属性只有两个:返回成成功标识:success、可以接收任意类型参数的msg。

@interface STModel : STModelBase
@property (nonatomic, assign) BOOL success;
@property (retain, nonatomic) id<NSObject> msg;
//将msg转成dictionary返回
@property (retain, nonatomic,readonly) NSDictionary* msgDic;
//将msg转成string返回
@property (copy, nonatomic,readonly) NSString* msgString;
@end

接收的Json结果参数为:{success:true,msg:xxxxx}

其它参数,msgDic和msgString,是方面把msg进行类型转换返回数据。

用法示例1:结果转字典,再从字典里拿数据转数组。

 

 用法示例2:结果转其它Model(自已的Model要继承自STModelBase)

 

 

4、STModelBase:用户自定义Model继承的基类,可以和STModel直接转换。

基本定义:

@interface STModelBase : JSONModel
-(id)initWithObject:(id<NSObject>)msg;
@end

只有构造函数一个,从STModel的结果中实例化对象(数据)

复杂的定义示例:

@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

本文到此结束。

标签:NSInteger,10,nonatomic,入门教程,copy,实体,NSString,property,assign
来源: https://www.cnblogs.com/cyq1162/p/13485213.html