| ページ一覧 | ブログ | twitter |  書式 | 書式(表) |

MyMemoWiki

差分

ナビゲーションに移動 検索に移動
560 バイト追加 、 2021年4月27日 (火) 13:24
}
</pre>
*書き出し処理読み込み、コレクションに追加、書き出し処理
<pre>
func addHostListDocument(host: WoL.Host) {
let docPath = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
let filePath = URL(fileURLWithPath: "hosts.json", relativeTo:docPath)
var hostList: [WoL.Host] = []
let decoder = JSONDecoder()
do {
let rawData = try Data(contentsOf: filePath)
print(rawData)
let tmpHostList = try decoder.decode([WoL.Host].self, from: rawData)
for tmpHost in tmpHostList {
hostList.append(tmpHost)
}
} catch {
print(error)
}
hostList.append(host)
let encoder = JSONEncoder()
do {
let line = try encoder.encode(hosthostList)
try line.write(to: filePath)
} catch {
print(error)
}
}
</pre>
[[File:swift_json_encode.png|300px]]

案内メニュー