YADMLib 0.2.0

YADMLib 0.2.0

测试已测试
语言语言 Obj-CObjective C
许可证 MIT
发布最后发布2017年4月

Csongor Nagy 维护。



YADMLib 0.2.0

  • Csongor Nagy

YADMLib 是一个轻量级的网络库,具有 Objective C 的 JSON 到数据模型解析功能。

要求

  • ARC 仅限;iOS 6.1+

安装

1) 源文件

  1. 将 YADMLib 仓库作为 zip 文件 下载,或克隆它
  2. 将 YADMLib 子文件夹复制到您的 Xcode 项目中

2) 通过 Cocoa Pod

在项目 Podfile 中添加 YADMLib Pod

target "YourProject" do
	pod 'YADMLib'
end

如果您想了解更多关于 CocoaPods 的信息,请参阅此 简短的教程

简单实现

OpenweatherMap 上注册账号,并在 YADMLibTester 应用程序的 Info.plist 中使用您的 API 密钥。最后,从 OpenweatherMap 获取一些 JSON,并将其映射到 OpenWeatherAPI 模型中。

我们的基本 OpenWeatherAPI 模型看起来像这样

#import "YADMJSONApiModel.h"

@protocol OpenWeatherAPI <NSObject>
@end

@interface OpenWeatherAPI : YADMJSONApiModel <OpenWeatherAPI>

@property (nonatomic, strong) NSString                          *base;
@property (nonatomic, strong) NSNumber                          *dt;
@property (nonatomic, strong) NSNumber                          *id;
@property (nonatomic, strong) NSString                          *name;
@property (nonatomic, strong) NSNumber                          *cod;

@end

现在我们通过控制器中的 OpenWeatherAPI 类调取 Openweather API

#import "OpenWeatherAPI.h"

@interface ViewController ()
@property (nonatomic, strong) OpenWeatherAPI    *weather;
@end

@implementation ViewController

-(void)viewDidAppear:(BOOL)animated
{
  [super viewDidAppear:animated];
  __weak typeof(self) weakSelf = self;
  self.weather = [[OpenWeatherAPI alloc] initWithURL:[NSURL URLWithString:@"http://api.openweathermap.org/data/2.5/weather"]
                                              method:@"GET"
                                              params:@{@"q"       : @"Berlin,de",
                                                       @"units"   : @"metric",
                                                       @"appid"   : openweathermapAPPId}
                                             headers:nil
                                         cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
                                             timeout:30
                                  andCompletionBlock:^(NSString *parsedJson, NSError *error) {
                                      __strong typeof(self) strongSelf = weakSelf;
                                      if (!error && strongSelf.weather)
                                      {
                                          [strongSelf testTimeLoaded];
                                      }
                                  }];
}

- (void)testTimeLoaded
{
    NSLog(@"City: %@", self.weather.name);
}

@end

高级实现

Openweather API 返回的是一个复杂的 JSON,有嵌套的对象和数组。使用 YADMLib,所有这些数据都会自动填充,您只需映射类结构即可。

我们的返回对象应该看起来像这样

#import "YADMJSONApiModel.h"
#import "OpenWeatherAPICoord.h"
#import "OpenWeatherAPISys.h"
#import "OpenWeatherAPIWeather.h"
#import "OpenWeatherAPIMain.h"
#import "OpenWeatherAPIWind.h"
#import "OpenWeatherAPIClouds.h"

@protocol OpenWeatherAPI <NSObject>
@end

@interface OpenWeatherAPI : YADMJSONApiModel <OpenWeatherAPI>

@property (nonatomic, strong) OpenWeatherAPICoord               *coord;
@property (nonatomic, strong) OpenWeatherAPISys                 *sys;
@property (nonatomic, strong) NSArray<OpenWeatherAPIWeather>    *weather;
@property (nonatomic, strong) NSString                          *base;
@property (nonatomic, strong) OpenWeatherAPIMain                *main;
@property (nonatomic, strong) OpenWeatherAPIWind                *wind;
@property (nonatomic, strong) OpenWeatherAPIClouds              *clouds;
@property (nonatomic, strong) NSNumber                          *dt;
@property (nonatomic, strong) NSNumber                          *id;
@property (nonatomic, strong) NSString                          *name;
@property (nonatomic, strong) NSNumber                          *cod;

@end

其中 *coordOpenWeatherAPICoord

#import "YADMJSONApiModel.h"

@protocol OpenWeatherAPICoord <NSObject>
@end

@interface OpenWeatherAPICoord : YADMJSONApiModel <OpenWeatherAPICoord>

@property (nonatomic, strong) NSNumber      *lon;
@property (nonatomic, strong) NSNumber      *lat;

@end

并且 *weatherOpenWeatherAPIWeather 类的 NSArray

#import "YADMJSONApiModel.h"

@protocol OpenWeatherAPIWeather <NSObject>
@end

@interface OpenWeatherAPIWeather : YADMJSONApiModel <OpenWeatherAPIWeather>

@property (nonatomic, strong) NSNumber      *id;
@property (nonatomic, strong) NSString      *main;
@property (nonatomic, strong) NSString      *description;
@property (nonatomic, strong) NSString      *icon;

@end

其他信息

作者

Csongor Nagy

许可证

本软件是在 MIT 许可证的条款和条件下分发的。

贡献指南

注意! 如果你修复了你发现的错误或有一些开发想法,请随意提交一个pull request。