API (Application Programming Interface) is a set of commands, function, protocols, objects that programmers can use to create software or interact with an external system.

  1. 공식 문서를 통해서 가입하고 확인할 것.

How to networking with Web Server?

<aside> 💡 내 앱과 웹서버와의 통신 (request, response)을 어떻게 할까?

</aside>

four steps to networking

  1. Create a URL
    1. use URL(string: string)
  2. Create a URLSession
  3. Give URLSession a task
  4. Start the task
func performRequest(urlString: String) {
    //1. create a url
    if let url = URL(string: urlString) {
        //2. creata a url session
        let session = URLSession(configuration: .default)
        //3. give the session a task
        let task =  session.dataTask(with: url, completionHandler: handle(data:response:error:))
        //handler는 비동기처리를 위한 함수.
        //4. start the task
        print("start the task")
        task.resume()
    }
}

func handle(data: Data?, response: URLResponse?, error: Error?) -> Void {
    if error != nil {
        print(error!)
        return
    }
    print("비동기처리 핸들러 데이터가 오기전까지 실행안됨")
    
    if let safeData = data {
        let dataString = String(data: safeData, encoding: .utf8)
        print(dataString)
    }
}

URLSession

https://developer.apple.com/documentation/foundation/urlsession

JSON parsing

JSON (JavaScript Object Notation)

we initialize swift object with either a struct or a class