为Sky定义model

这一节,我们根据之前设计的UI,设计对应的Model。

DarkSky API

首先,来了解一个简单好用的天气API服务:DarkSky。简单注册登录之后,打开Console页面就会看到一个Secret Key,以及对应的获取天气信息的方法:

DarkSkyAndModel

在图中可以看到,我们只要发送GET请求到:https://api.darksky.net/forecast/your_secret_key/latitude,longitude,就可以请求到天气信息了。当然,这个API还支持一些可选的参数,大家可以在这里找到详细的API参数。

而DarkSky返回的数据格式,大体是这样的:

{
    "latitude": 37.8267,
    "longitude": -122.4233,
    "timezone": "America/Los_Angeles",
    "currently": {
        "time": 1506417757,
        "summary": "Clear",
        "icon": "clear-night",
        "nearestStormDistance": 482,
        "nearestStormBearing": 29,
        "precipIntensity": 0,
        "precipProbability": 0,
        "temperature": 61.23,
        "apparentTemperature": 61.23,
        "dewPoint": 48.81,
        "humidity": 0.64,
        "pressure": 1010.41,
        "windSpeed": 1.76,
        "windGust": 2.97,
        "windBearing": 287,
        "cloudCover": 0,
        "uvIndex": 0,
        "visibility": 10,
        "ozone": 293.28
    },
    "minutely": {
        "summary": "Clear for the hour.",
        "icon": "clear-night",
        "data": [...]
    },
    "hourly": {
        "summary": "Clear throughout the day.",
        "icon": "clear-day",
        "data": [...]
    },
    "daily": {
        "summary": "Light rain on Monday, with temperatures falling to 69°F on Monday.",
        "icon": "rain",
        "data": [...]
    },
    "flags": {
    "sources": [...],
    "isd-stations": [...],
    "units": "us"
    },
    "offset": -7
}

大家可以在这里每个参数的详细说明。这里,我们简单说一下会用到的部分:

  • currently / minutely / hourly / daily节点包含的内容,分别表示当前,按分钟、按小时以及按天统计的天气数据。我们的App首页上半部分,就会用到currently中的内容;在稍后,我们实现首页下半部分的内容时,就会用到daily中的内容;
  • currently中,我们需要以下字段的内容:
    • time:当前时间;
    • summary:当前天气简述;
    • icon:天气图标名称,在Xcode的Assets里,我们使用的每一个图片,都和这里的图标名称是一一对应的;
    • temperature:当前温度(默认采用华氏度单位);
    • humidity:湿度;

因此,简单来说,我们需要的JSON暂时是这样的:

{
    "latitude": 37.8267,
    "longitude": -122.4233,
    "timezone": "America/Los_Angeles",
    "currently": {
        "time": 1506417757,
        "summary": "Clear",
        "icon": "clear-night",
        "temperature": 61.23,
        "humidity": 0.64
    }
}

添加API配置信息

了解了DarkSky的API格式之后,为了使用这个API,我们要添加一些接口信息。为此,在Sky group里,新建一个Configuration group,所有第三方服务的配置,都会添加在这里。现在,我们先在其中创建一个Configuratioin.swift文件,并添加下面的代码:

struct API {
    static let key = "your_secret_key_here"
    static let baseUrl = URL(string: "https://api.darksky.net/forecast")!
    static let authenticatedUrl = baseUrl.appendingPathComponent(key)
}

这样,我们就能在整个项目里,用统一的访问访问DarkSky的API了。

设计Model

添加好配置之后,我们就可以设计对应的Model了。在项目的Sky group中,新建一个Models group,我们需要创建两个models。

一个model表示当前的位置和地区名称。我们可以通过CoreLocation获取到。新建一个Location.swift文件,并添加下面的代码:

struct Location {
    var name: String
    var latitude: Double
    var longitude: Double
}

由于稍后,我们会用到定位功能,因此记得在Info.plist中添加下面的内容向用户申请权限:

<key>NSLocationWhenInUseUsageDescription</key>
<string>Sky needs your location to fetch the weather data.</string>

另一个model则表示天气数据,这是DarkSky的返回值。新建一个WeatherData.swift文件,并添加下面的代码:

struct WeatherData: Codable {
    let latitude: Double
    let longitude: Double
    let currently: CurrentWeather

    struct CurrentWeather: Codable {
        let time: Date
        let summary: String
        let icon: String
        let temperature: Double
        let humidity: Double
    }
}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 11,161评论 1 32
  • 1.网络 1.网络七层协议有哪些? 物理层:主要功能:传输比特流;典型设备:集线器、中继器;典型协议标准和应用:V...
    _我和你一样阅读 3,507评论 1 38
  • 自定义Gradle插件主要有三种形式,分别是build.gradle中编写、buildSrc工程项目中编写、独立项...
    Yolyn阅读 1,211评论 0 3
  • Django Model 定义语法 版本:1.7主要来源:https://docs.djangoproject.c...
    罗田阅读 31,089评论 2 42
  • 2015/04/22更新:最新更新到Xcode6.3和Swfit 1.2。 更新日志:这个教程由Vicent Ng...
    木易林1阅读 343评论 0 0