はじめに
設定ファイルなどのシンプルなデータをアプリ内に保存し、必要に応じて読み込みたいようなケースでは、JSON形式で取り扱うと簡単に行えます。
今回はUnboxとWrapという2つのライブラリを使ったSwiftの簡単な実装例を紹介します。
ライブラリの紹介
Unbox、WrapともにJohn Sundell氏が公開しているJSON decoder/encoderです(GitHubはこちら)
前提条件
- Xcode 9.0
- Swift 4.0
- Unbox 2.5.0
- Wrap 2.1.1
事前準備
UnboxとWrapをXcodeプロジェクトに追加します。
UnboxとWrapはCocoaPodsやCarthageなどのメジャーなツールに対応しています。
Carthageを利用する場合はこちらを参考にライブラリを追加してください。
実装
データの準備
JSON化するデータを定義します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | importUnbox structSetting{ letsound:Sound letnotification:Notification structSound{ letbgmVolume:Float letseVolume:Float letmute:Bool } structNotification{ letgeneral:Bool letevent:Bool letrecommend:Bool } } |
設定データをテキストファイルに出力する
設定データをJSON化してテキストファイルに保存します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | importUIKit importWrap classViewController: UIViewController{ overridefuncviewDidLoad(){ super.viewDidLoad() // 設定データの準備 letsound=Setting.Sound(bgmVolume:1.0,seVolume:1.0,mute:false) letnotification=Setting.Notification(general:true,event:true,recommend:true) letsetting=Setting(sound:sound,notification:notification) // セーブ save(setting) } overridefuncdidReceiveMemoryWarning(){ super.didReceiveMemoryWarning() } funcsave(_setting:Setting){ // 保存するパスの指定 letpath=NSHomeDirectory()+"/Documents/setting.txt" leturl=URL(fileURLWithPath:path) do{ // 設定データをData型に変換してsetting.txtに出力する letdata:Data=trywrap(setting) trydata.write(to:url,options:Data.WritingOptions.atomic) }catch{ } } } |
設定データの読み込み
JSONをdecodeするための処理を追加します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | importUnbox structSetting{ // 省略 } // 追加 extensionSetting: Unboxable{ init(unboxer:Unboxer)throws{ self.sound=unboxer.unbox(key:"sound") self.notification=unboxer.unbox(key:"notification") } } extensionSetting.Sound: Unboxable{ init(unboxer:Unboxer)throws{ self.bgmVolume=unboxer.unbox(key:"bgmVolume") self.seVolume=unboxer.unbox(key:"seVolume") self.mute=unboxer.unbox(key:"mute") } } extensionSetting.Notification: Unboxable{ init(unboxer:Unboxer)throws{ self.general=unboxer.unbox(key:"general") self.event=unboxer.unbox(key:"event") self.recommend=unboxer.unbox(key:"recommend") } } |
setting.txtから設定データを読み込みます。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | importUIKit importWrap classViewController: UIViewController{ overridefuncviewDidLoad(){ super.viewDidLoad() // 設定データの読み込み ifletsetting=load(){ }else{ // 設定データの準備 letsound=Setting.Sound(bgmVolume:1.0,seVolume:1.0,mute:false) letnotification=Setting.Notification(general:true,event:true,recommend:true) letsetting=Setting(sound:sound,notification:notification) // セーブ save(setting) } } // 省略 funcload()->Setting?{ // setting.txtのパスの指定 letpath=NSHomeDirectory()+"/Documents/setting.txt" leturl=URL(fileURLWithPath:path) do{ // Data型からSetting型に変換する letdata=tryData(contentsOf:url) letsetting:Setting=tryunbox(data:data) returnsetting }catch{ returnnil } } } |
さいごに
UnboxとWrapを利用して設定ファイルを永続化するSwiftのコードを紹介しました。
保存するデータ量の多いアプリを作る機会があればRealmを使ってみたいと思います。