LoginSignup
0
0

More than 5 years have passed since last update.

APIGatewayのGenerated SDKをSwift4に対応させる

Last updated at Posted at 2017-12-08

スクリーンショット 2017-12-08 20.55.01.png

今まで普通に使えていたAPIGatewayのGenerated SDK(AWSのコンソールから生成してダウンロードするやつ)がエラーを吐くようになった。どうやらSwift4へ一時的に対応していないようだったので、いろいろ調べてどうにか対応させた。

どんなエラーが出るようになったか

  1. レスポンスをパースする時に(プロパティ名) is not a property of (モデル名)というエラー
  2. 配列のレスポンスをパースしたときに「fatal error: NSArray element failed to match the Swift Array Element type」というエラー

対処法

対処法は、各モデルを定義してるSwiftファイルで、全てのプロパティと「(プロパティ名)JSONTransformer」のクラスの先頭に「@objc」をつけること。Swift4からこれを明示的につけないとObjective-Cのコードから当該プロパティやクラスを見つけられなくなっているようでした。

APIGUser.swift
import Foundation
import AWSCore


public class APIGUser: AWSModel {

    //全てのプロパティの先頭に「@objc」
    @objc var username: String?
    @objc var devices: [APIGUser_item]?

    public override static func jsonKeyPathsByPropertyKey() -> [AnyHashable : Any]!{
        var params:[AnyHashable : Any] = [:]
        params["username"] = "username"
        params["devices"] = "devices"

        return params
    }

     //JSONTransformerにも@Objc
    @objc class func devicesJSONTransformer() -> ValueTransformer{
        return  ValueTransformer.awsmtl_JSONArrayTransformer(withModelClass: APIGUser_devices_items.self);
    }
}

参考

Support Swift 4 for API Gateway

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0