[swift] Swift ObjectMapper를 사용한 예시 코드를 알려주세요.

먼저, ObjectMapper를 설치하고 import해야 합니다. 이후에는 ObjectMapper를 사용하여 JSON 데이터를 객체로 매핑할 수 있습니다.

import ObjectMapper

struct User: Mappable {
    var name: String?
    var age: Int?

    init?(map: Map) {}

    mutating func mapping(map: Map) {
        name <- map["name"]
        age <- map["age"]
    }
}

let jsonString = """
{
    "name": "John",
    "age": 30
}
"""

if let user = User(JSONString: jsonString) {
    print("Name: \(user.name ?? "")")
    print("Age: \(user.age ?? 0)")
}

위의 코드에서는 ObjectMapper를 사용하여 JSON 문자열을 User 객체로 변환하는 예시입니다. User 구조체는 Mappable 프로토콜을 구현하고 있으며, mapping(map:) 메서드를 통해 JSON 필드와 객체의 속성을 매핑합니다.

JSON 문자열을 User 객체로 변환하기 위해 User(JSONString: jsonString)을 사용합니다. 변환된 User 객체를 통해 이름과 나이를 출력합니다.

자세한 내용은 ObjectMapper의 공식 문서를 참고하시기 바랍니다.