[swift] Swift에서 Codable과 Alamofire를 사용하여 JSON 데이터 읽기 및 쓰기

개요

Swift는 Codable 프로토콜을 통해 데이터를 JSON 형식으로 쉽게 변환하고 읽고 쓸 수 있습니다. 이를 통해 JSON 데이터를 가져오거나 서버에 전송하는 등의 작업을 효율적으로 처리할 수 있습니다. Alamofire는 Swift에서 인기있는 HTTP 통신 라이브러리이며, Alamofire와 함께 사용하면 JSON 데이터의 불러오기와 전송을 더욱 편리하게 처리할 수 있습니다.

Codable 프로토콜

Codable 프로토콜을 사용하면 Swift에서 데이터를 JSON으로 변환하고 JSON에서 데이터로 다시 변환할 수 있습니다. Codable 프로토콜을 채택한 구조체나 클래스는 JSONEncoderJSONDecoder를 사용하여 JSON 데이터로 변환하거나 JSON 데이터를 객체로 복원할 수 있습니다.

struct Person: Codable {
    var name: String
    var age: Int
}

let person = Person(name: "John", age: 30)

// 객체를 JSON 데이터로 변환
let jsonEncoder = JSONEncoder()
if let jsonData = try? jsonEncoder.encode(person) {
    let jsonString = String(data: jsonData, encoding: .utf8)
    print(jsonString)
    // 출력: {"name":"John","age":30}
}

// JSON 데이터를 객체로 변환
let json = """
{
    "name": "Jane",
    "age": 25
}
"""

let jsonDecoder = JSONDecoder()
if let jsonData = json.data(using: .utf8), let decodedPerson = try? jsonDecoder.decode(Person.self, from: jsonData) {
    print(decodedPerson.name) // 출력: Jane
    print(decodedPerson.age) // 출력: 25
}

Alamofire를 사용하여 JSON 데이터 읽기

Alamofire는 Swift에서 HTTP 통신을 더욱 편리하게 처리할 수 있는 라이브러리입니다. Alamofire를 사용하여 API 요청을 보내고 서버로부터 JSON 데이터를 읽어올 수 있습니다.

import Alamofire

AF.request("https://api.example.com/users")
    .responseJSON { response in
        switch response.result {
        case .success(let value):
            if let users = value as? [[String: Any]] {
                for user in users {
                    if let name = user["name"] as? String,
                       let age = user["age"] as? Int {
                        print(name, age)
                    }
                }
            }
            
        case .failure(let error):
            print(error)
        }
    }

위 코드에서는 Alamofire의 request 함수를 사용하여 API 요청을 보냅니다. responseJSON 클로저 내에서 response.result를 통해 요청에 대한 응답 결과를 확인할 수 있습니다. 응답 결과가 성공인 경우, JSON 데이터를 사용할 수 있습니다. 위 예제에서는 응답 데이터를 [[String: Any]] 타입으로 캐스팅하여 사용하고 있습니다.

Alamofire를 사용하여 JSON 데이터 쓰기

Alamofire를 사용하여 JSON 데이터를 서버로 전송하는 것도 간단합니다. HTTPHeaders와 함께 request 함수를 사용하여 JSON 데이터를 서버로 전송할 수 있습니다.

import Alamofire

let parameters: [String: Any] = [
    "name": "John",
    "age": 30
]

AF.request("https://api.example.com/users", method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: nil)
    .responseJSON { response in
        switch response.result {
        case .success(let value):
            print(value)
            
        case .failure(let error):
            print(error)
        }
    }

위 코드에서는 parameters에 전송할 데이터를 딕셔너리 형태로 설정하고, request 함수의 method 매개변수를 .post로 지정하여 POST 요청을 보냅니다. JSON 데이터는 encoding 매개변수를 JSONEncoding.default로 지정하여 인코딩합니다.

결론

Swift에서 Codable과 Alamofire를 함께 사용하여 JSON 데이터를 쉽게 읽고 쓸 수 있습니다. Codable 프로토콜을 사용하여 Swift 객체를 JSON 데이터로 변환하고, JSON 데이터를 Swift 객체로 변환할 수 있습니다. Alamofire를 사용하여 API 요청을 보내고 서버로부터 JSON 데이터를 읽거나 JSON 데이터를 서버로 전송할 수 있습니다. 이를 통해 효율적인 JSON 데이터 처리를 할 수 있습니다.

참고 자료