[iOS]Playground에서NASA(미국항공우주국)의 오픈 데이터를 분석하여 달의 이미지를 얻는 방법

10111 단어 SwiftiOS

Playground에서 NASA(미국항공우주국)의 개방 데이터를 분석하여 달의 이미지를 얻는 방법



【사진】NASA(미국항공우주국)의 개방 데이터에서 얻은 달 이미지(말리스츄)
Image Credit : Public Domain
https://apod.nasa.gov/apod/image/1710/MariusHills_LO2LRO_1673.jpg

공유


"Playground로 API 해석하기(NASA 오픈 데이터의 경우)"(2017년 10월 24일 Qiita 공개)의 내용을 Playground로 API를 더 자세히 해석하는 방법

참고 자료


App Development with Swift - Apple
Playground를 사용하여 API 구문 분석(NASA 오픈 데이터의 경우)

개발 환경


Mac OS High Sierra/Swift 4.0/Xcode 9.0

전제 조건


NASA Open APIs( https://api.nasa.gov/index.html#getting-started ), 그리고 API 키, 다음 코드 예시의 YOURAPI_KEY 대입해주세요.

코드 예


Myplayground.playground
import UIKit
import PlaygroundSupport

PlaygroundPage.current.needsIndefiniteExecution = true

struct PhotoInfo: Codable{
    var title: String
    var description: String
    var hdurl: String

    enum Keys: String, CodingKey{
        case title = "title"
        case description = "explanation"
        case hdurl = "hdurl"
    }

    init(from decoder: Decoder) throws {
        let valueContainer = try decoder.container(keyedBy: Keys.self)
        self.title = try valueContainer.decode(String.self,forKey: Keys.title)
        self.description = try valueContainer.decode(String.self,forKey: Keys.description)
        self.hdurl = try valueContainer.decode(String.self,forKey: Keys.hdurl)
    }
}

let url = URL(string: "https://api.nasa.gov/planetary/apod?api_key=YOUR_API_KEY")!

let task = URLSession.shared.dataTask(with: url){
    (data, response, error) in
    let jsonDecoder = JSONDecoder()
    if let data = data,
        let photoInfo = try? jsonDecoder.decode(PhotoInfo.self, from: data){
        print("-------------")
        print(photoInfo.title)
        print("-------------")
        print(photoInfo.description)
        print("-------------")
        print(photoInfo.hdurl)
        print("-------------")
    } else {
        print("Either no data was returned, or data was not serialized.")
    }
}


task.resume()

실행 결과


실행 결과
-------------
Marius Hills and a Hole in the Moon
-------------
Could humans live beneath the surface of the Moon? This intriguing possibility was bolstered in 2009 when Japan's Moon-orbiting SELENE spacecraft imaged a curious hole beneath the Marius Hills region on the Moon, possibly a skylight to an underground lava tube. Follow-up observations by NASA's Lunar Reconnaissance Orbiter (LRO) indicated that the Marius Hills Hole (MHH) visually extends down nearly 100 meters and is several hundred meters wide.  Most recently, ground penetrating radar data from SELENE has been re-analyzed to reveal a series of intriguing second echoes -- indicators that the extensive lava tubes exist under Marius Hills might extend down even kilometers and be large enough to house cities.  Such tubes could shelter a future Moon colony from large temperature swings, micro-meteor impacts, and harmful solar radiation.  Potentially, underground lava tubes might even be sealed to contain breathable air.  These lava tubes likely formed when lunar volcanos were active billions of years ago. Pictured, the surface of Marius Hills region was captured in the 1960s by NASA's Lunar Orbiter 2 mission, while an inset image of the MHH is shown from NASA's continuing LRO.  Several volcanic domes are visible, while Marius Crater is visible on the upper right.
-------------
https://apod.nasa.gov/apod/image/1710/MariusHills_LO2LRO_1673.jpg
-------------

주안점


Swift4.0을 사용한 Codable 분석 API
자세한 내용은 아래 링크를 참조하십시오.
Encoding and Decoding Custom Types - Apple

좋은 웹페이지 즐겨찾기