【Swift】 JSONEncoder / JSONDecorder の基本的な使い方

Apple 公式リファレンスから。サンプルも抜粋。


func encode<T>(_ value: T) throws -> Data where T : Encodable


func decode<T>(
    _ type: T.Type,
    from data: Data
) throws -> T where T : Decodable

👉 encode(_:) | Apple Developer Documentation hatena-bookmark
👉 decode(_:from:) | Apple Developer Documentation hatena-bookmark


struct GroceryProduct: Codable {
  var name: String
  var points: Int
  var description: String?
}

let pear = GroceryProduct(name: "Pear", points: 250, description: "A ripe pear.")

let encoder = JSONEncoder()
encoder.outputFormatting = .prettyPrinted

let data = try encoder.encode(pear)
print(String(data: data, encoding: .utf8)!)

/*
{
  "name" : "Pear",
  "points" : 250,
  "description" : "A ripe pear."
}
*/


struct GroceryProduct: Codable {
  var name: String
  var points: Int
  var description: String?
}

let json = """
{
    "name": "Durian",
    "points": 600,
    "description": "A fruit with a distinctive scent."
}
""".data(using: .utf8)!

let decoder = JSONDecoder()
let product = try decoder.decode(GroceryProduct.self, from: json)

print(product.name) 

// Prints "Durian"

👉 JSONEncoder | Apple Developer Documentation hatena-bookmark
👉 JSONDecoder | Apple Developer Documentation hatena-bookmark

Codable というのは、Encodable + Decodable のこと。


typealias Codable = Decodable & Encodable

👉 Codable | Apple Developer Documentation hatena-bookmark

 

🧑🏻‍💻 まとめ

まとめると以下のイメージ。

Data と String の相互変換は以下。


さらに、キー名のカスタムをしたい場合は以下から。

👉 Encoding and Decoding Custom Types | Apple Developer Documentation hatena-bookmark



【Swift】 文字コードの変換 を整理してみた

前にやってみた、これ。


2つの型(プロトコル)の間を相互に変換するだけでも、いろんな書き方があるのだな。

などと思いつつ気になってたのが文字コードの変換。

👉 文字コード - Wikipedia hatena-bookmark

今回、おおまかに整理しておきたい。

 

🧑🏻‍💻 String の内部エンコーディングは UTF-8

Swift 5 switches the preferred encoding of strings from UTF-16 to UTF-8 while preserving efficient Objective-C-interoperability.

👉 Swift.org - UTF-8 String hatena-bookmark

String 内部のエンコーディングは「UTF-8」になったらしいです。

 

🧑🏻‍💻 String、 Data とファイル間での相互変換

図で。大体こんな。

まずは、エンコーディングの引数名が、encoding:, using:, as: と多様なので初見、分かりづらかった。


「String から」または「Stringへ」変換する場合に

「相手」 (「変換先」または「変換元」) のエンコーディングを指定する。

String 自体は「UTF-8」として意識。

Data と ファイル間の変換にはエンコーディングの指定は不要。

Data はそれぞれに変換済みのバイトシーケンス。

 

🧑🏻‍💻 変換してみる

コードを書いて試してみます。

👉 【Swift】よく使いそうな String の format hatena-bookmark


"あいうえお"
  .data(using: .shiftJIS)!.map { String(format: "%02X", $0) }.joined()
// 10 bytes 82A082A282A482A682A8

からスタートして一通り。

以前書いた extension を使っています。

👉 【Swift】ファイルやディレクトリ操作するための extension をまずは作った hatena-bookmark

これで大体いけるはず。

注意としては、「変換できない文字もある」ということ。


o  Shift_JIS → UTF-8 
x  UTF-8     → Shift_JIS

対応する文字がない。

 

🧑🏻‍💻 参考

👉 String.Encoding | Apple Developer Documentation hatena-bookmark
👉 shiftJIS | Apple Developer Documentation hatena-bookmark
👉 init(data:encoding:) | Apple Developer Documentation hatena-bookmark
👉 init(contentsOf:encoding:) | Apple Developer Documentation hatena-bookmark
👉 data(using:allowLossyConversion:) | Apple Developer Documentation hatena-bookmark
👉 write(to:atomically:encoding:) | Apple Developer Documentation hatena-bookmark
👉 init(contentsOf:options:) | Apple Developer Documentation hatena-bookmark
👉 write(to:options:) | Apple Developer Documentation hatena-bookmark



【Swift】複数要素の並び替えには Tuple を使うとよい

こんなデータがあったとして、


struct User {
  var last: String
  var first: String
  var age: Int
}

let users = [
  User(last: "さとう", first: "はると", age: 19),
  User(last: "さとう", first: "しげる", age: 46),
  User(last: "いとう", first: "はると", age: 15),
  User(last: "いとう", first: "しげる", age: 50)
]

users
  .forEach { user in
    print("\(user.last)\(user.first) \(user.age)")
  }


さとうはると 19
さとうしげる 46
いとうはると 15
いとうしげる 50

並び替えたいですよね。

Tuple を使うとこんな書き方ができるんですね。


users
  .sorted(
    by: {
      ($0.last, $0.first, $0.age) < ($1.last, $1.first, $1.age)
    }
  )
  .forEach {
    print("\($0.last)\($0.first) \($0.age)")
  }


いとうしげる 50
いとうはると 15
さとうしげる 46
さとうはると 19

便利です。

少し書き換えて、年齢順で。


users
  .map {
    ($0.age, $0.last, $0.first)
  }
  .sorted(
    by: {
      $0 < $1
    }
  )
  .forEach {
    print("\($0.1)\($0.2) \($0.0)")
  }


いとうはると 15
さとうはると 19
さとうしげる 46
いとうしげる 50

これで、並び替えは自在ですね!

👉 [Swift] Tuple タプルの七不思議 #tuple - Qiita hatena-bookmark