首先,在IOS中采用MVC设计模式可以有效地解耦、优化代码结构以及方便代码管理。在读取服务器接口JSON数据时,我们可以采用以下步骤:
- 创建一个Model类:定义与服务器端数据对应的模型,一般以属性的形式表示。
@interface User : NSObject
@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) NSString *age;
@property (nonatomic, strong) NSString *sex;
@end
- 创建一个网络请求类:向服务器端发送请求,获取数据。
- (void)getUserInfo:(NSString *)userId completion:(void (^)(User *user))completion{
NSString *urlString = [NSString stringWithFormat:@"http://xxxxx/getUserInfo?userId=%@", userId];
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSURLSessionDataTask *task = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if (data) {
NSError *parseError;
NSDictionary *userInfo = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&parseError];
if (!parseError) {
User *user = [[User alloc] init];
user.name = userInfo[@"name"];
user.age = userInfo[@"age"];
user.sex = userInfo[@"sex"];
if (completion) {
completion(user);
}
}
}
}];
[task resume];
}
- 创建一个Controller类:控制获取到的数据如何展示
- (void)showUserInfo:(NSString *)userId {
[networkManager getUserInfo:userId completion:^(User *user) {
NSString *name = user.name;
NSString *age = user.age;
NSString *sex = user.sex;
NSLog(@"name:%@\nage:%@\nsex:%@", name, age, sex);
}];
}
以上步骤基本上可以满足大部分 IOS-MVC层读取服务器接口JSON数据 的需求。接下来,通过两个示例进行详细说明:
示例1:获取用户列表数据并展示
模型类:UserList,包含一个数组,每个元素是一个 User 对象
@interface UserList : NSObject
@property (nonatomic, strong) NSMutableArray *userList;
@end
@interface User : NSObject
@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) NSString *age;
@property (nonatomic, strong) NSString *sex;
@end
网络请求类:获取用户列表数据
- (void)getUserList:(void (^)(UserList *userList))completion{
NSString *urlString = @"http://xxxxx/getUserList";
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSURLSessionDataTask *task = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if (data) {
NSError *parseError;
NSDictionary *userListInfo = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&parseError];
if (!parseError) {
NSMutableArray *userList = [NSMutableArray array];
for (NSDictionary *userInfo in userListInfo[@"data"]) {
User *user = [[User alloc] init];
user.name = userInfo[@"name"];
user.age = userInfo[@"age"];
user.sex = userInfo[@"sex"];
[userList addObject:user];
}
UserList *list = [[UserList alloc] init];
list.userList = userList;
completion(list);
}
}
}];
[task resume];
}
控制器类:展示获取到的用户列表数据
- (void)showUserList {
[networkManager getUserList:^(UserList *userList) {
for (User *user in userList.userList) {
NSString *name = user.name;
NSString *age = user.age;
NSString *sex = user.sex;
NSLog(@"name:%@\nage:%@\nsex:%@", name, age, sex);
}
}];
}
示例2:获取新闻列表数据并展示
模型类:NewsList,包含一个数组,每个元素是一个 News 对象
@interface NewsList : NSObject
@property (nonatomic, strong) NSMutableArray *newsList;
@end
@interface News : NSObject
@property (nonatomic, strong) NSString *title;
@property (nonatomic, strong) NSString *content;
@property (nonatomic, strong) NSString *time;
@end
网络请求类:获取新闻列表数据
- (void)getNewsList:(void (^)(NewsList *newsList))completion{
NSString *urlString = @"http://xxxxx/getNewsList";
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSURLSessionDataTask *task = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if (data) {
NSError *parseError;
NSDictionary *newsListInfo = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&parseError];
if (!parseError) {
NSMutableArray *newsList = [NSMutableArray array];
for (NSDictionary *newsInfo in userListInfo[@"data"]) {
News *news = [[News alloc] init];
news.title = newsInfo[@"title"];
news.content = newsInfo[@"content"];
news.time = newsInfo[@"time"];
[newsList addObject:news];
}
NewsList *list = [[NewsList alloc] init];
list.newsList = newsList;
completion(list);
}
}
}];
[task resume];
}
控制器类:展示获取到的新闻列表数据
- (void)showNewsList {
[networkManager getNewsList:^(NewsList *newsList) {
for (News *news in newsList.newsList) {
NSString *title = news.title;
NSString *content = news.content;
NSString *time = news.time;
NSLog(@"title:%@\ncontent:%@\ntime:%@", title, content,time);
}
}];
}
以上是 IOS-MVC层读取服务器接口JSON数据 的完整攻略,通过以上步骤可以快速创建一个网络请求以及相应的模型与展示代码。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:IOS-MVC层读取服务器接口JSON数据 - Python技术站