YADMLib 是一个轻量级的网络库,具有 Objective C 的 JSON 到数据模型解析功能。
在项目 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
其中 *coord
是 OpenWeatherAPICoord
类
#import "YADMJSONApiModel.h"
@protocol OpenWeatherAPICoord <NSObject>
@end
@interface OpenWeatherAPICoord : YADMJSONApiModel <OpenWeatherAPICoord>
@property (nonatomic, strong) NSNumber *lon;
@property (nonatomic, strong) NSNumber *lat;
@end
并且 *weather
是 OpenWeatherAPIWeather
类的 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。