LoginSignup
1
0

More than 5 years have passed since last update.

APIKitでplistファイルを読み込む

Posted at

APIKitでplistの読み込みをする必要があったので、以下のような対応をしました。

DataParserを定義

APIKitはデフォルトでJSONを扱うようになっていると思いますが、DataParserをオーバーライドできるので、どのようなデータ形式も読み込めると思います。
plistはiOSにPropertyListSerializationというクラスがあるので、これで一発でいけます。

final class PlistDataParser: DataParser {
    var contentType: String? {
        return "application/x-plist"
    }

    func parse(data: Data) throws -> Any {
        do {
            let options: PropertyListSerialization.ReadOptions = []
            let plist = try PropertyListSerialization.propertyList(from: data, options: options, format: nil)
            return plist
        } catch {
            debugPrint("error")
            return []
        }

    }
}

データのデコード部分はHimotokiを利用していましたが、plistのパースのやり方がわからなかったので、そこは自力でパースさせました。

個人的にplistは巨大なデータのパース速度がJSONに比べて早いのでたまに利用します。

1
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
1
0