独自Documentクラス
Cocoa勉強会 関東 Swift勉強会 2018-03
2018/3/16 Bitz Co., Ltd. 村上幸雄
• 埼玉県朝霞市でソフトハウスを起業。
• macOSやiOS、Androidのアプリケーション開発を主に請け負う。
• 自社アプリの製作。
• Twitter: @m_yukio
• Facebook: yukio.murakami
• GitHub: murakami
• http://www.bitz.co.jp/weblog/
BUKURO.swift
• macOS/iOS/watchOS/tvOSプログラマーのための勉強会。
初心者大歓迎。
• Cocoa勉強会 関東とMOSAの合同勉強会。
ほぼ毎月、池袋コワーキングスペース OpenOffice FOREST で開催。
• 申し込みは、connpassから。
https://cocoa-kanto.connpass.com/
• プログラマーが楽しくプログラミングできるようにサポートする場を
提供したい
• 勉強会に参加された方全員が学びを得られる様にサポートします!
• 勉強会に参加できない方にも学び得られるよう、勉強会の成果を可
能な限りアウトプットします!
• プログラマの拠り所となる場を目指します!
• PubHack.swiftは、飲み会イベントを通じて、
macOS/iOS/tvOS/watchOS/Androidエンジニアの人たちの交流を目的として
います。
• Cocoa勉強会 関東とMOSAの合同イベント。
• 申し込みは、connpassから。
https://cocoa-kanto.connpass.com/
Controller
View Model
Traditional version of MVC as a compound pattern
User action
Update
Update
Notify
Get changed state
Composite
Strategy
Observer
Controller
View Model
Cocoa version of MVC as a compound design pattern
User action
Update
Update
Notify
Command
Composite
Mediator
Strategy
Observer
NSViewControllerNSView
View Controller Model
NSDocument data
NSApplicationDelegate
ユーザー操作
event
更新
更新
更新
参照
通知
NSController
• iOS 5 からUIDocumentクラスが用意されたが、少々、オ
ーバースペック。
• 発表者は、以前から、独自のDocumentクラスを用意して
いた。
Documentクラスはモデル・コントローラ
class Document {
var version: String
private var _uniqueIdentifier: String
var uniqueIdentifier: String {
return _uniqueIdentifier
}
static let sharedInstance: Document = {
let instance = Document()
return instance
}()
init() {
let infoDictionary = Bundle.main.infoDictionary! as Dictionary
self.version = infoDictionary["CFBundleShortVersionString"]! as! String
self._uniqueIdentifier = ""
}
deinit {
}
func load() {
loadDefaults()
}
func save() {
updateDefaults()
}
private func clearDefaults() {
let userDefaults = UserDefaults.standard
if userDefaults.object(forKey: "version") != nil {
userDefaults.removeObject(forKey: "version")
}
if userDefaults.object(forKey: "uniqueIdentifier") != nil {
userDefaults.removeObject(forKey: "uniqueIdentifier")
}
}
private func updateDefaults() {
let userDefaults = UserDefaults.standard
var versionString: String = ""
if userDefaults.object(forKey: "version") != nil {
versionString = userDefaults.object(forKey: "version") as! String
}
if versionString.compare(self.version) != .orderedSame {
userDefaults.setValue(self.version, forKey: "version")
userDefaults.synchronize()
}
var uniqueIdentifier: String = ""
if userDefaults.object(forKey: "uniqueIdentifier") != nil {
uniqueIdentifier = userDefaults.object(forKey: "uniqueIdentifier") as! String
}
if uniqueIdentifier.compare(self.uniqueIdentifier) != .orderedSame {
userDefaults.setValue(self.uniqueIdentifier, forKey: "uniqueIdentifier")
userDefaults.synchronize()
}
}
private func loadDefaults() {
let userDefaults = UserDefaults.standard
var versionString: String = ""
if userDefaults.object(forKey: "version") != nil {
versionString = userDefaults.object(forKey: "version") as! String
}
if versionString.compare(self.version) != .orderedSame {
/* バージョン不一致対応 */
clearDefaults()
_uniqueIdentifier = UUID.init().uuidString
}
else {
/* 読み出し */
if userDefaults.object(forKey: "uniqueIdentifier") != nil {
_uniqueIdentifier = userDefaults.object(forKey: "uniqueIdentifier") as! String
}
}
}
private func modelDir() -> String {
let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
if paths.count < 1 {
return ""
}
var path = paths[0]
path = path.appending(".model")
return path
}
private func modelPath() -> String {
let path = modelDir().appending("/model.dat")
return path;
}
}
シングルトンを使わない例
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
public let document = Document()
:
}
let delegate = UIApplication.shared.delegate as! AppDelegate
let document = delegate.document
print(document)

More Related Content

PPTX
独自Documentクラス
PPTX
Bukuro.makers
PPTX
Document based application
PPTX
デザインパターン
PPTX
サーバーレスアーキテクチャで悩んでます
PPTX
ゲームの企画書づくりに挑戦
PPTX
The Bash in Tokyo : AppKitとUIKit
PPTX
独自Documentクラス
Bukuro.makers
Document based application
デザインパターン
サーバーレスアーキテクチャで悩んでます
ゲームの企画書づくりに挑戦
The Bash in Tokyo : AppKitとUIKit

What's hot (13)

PPTX
Getting a packet trace
PPTX
IBM Watson Services for Core ML
PPTX
Master-Detail App を実装する
PDF
部品に切り分けて考えるView構造とライブラリを上手に活用したUI実装
PDF
ライブラリやView構造を有効活用して iOSアプリのUIをオシャレにするワザ紹介
PDF
デザイナー→Webエンジニア→iOSエンジニアと渡り歩いた僕なりのSwiftとの向き合い方と生かす戦略
PDF
できるだけUI系のライブラリを用いないアニメーションを盛り込んだサンプル実装まとめ
PDF
Approach of Prototyping for making Application User Interface about iOS
PDF
Gulp WordPress @Grand-Frontend-Osaka 2015 Summer
PDF
SwiftとReactNativeで似たようなUIを作った際の記録
PDF
iOS側のUIの特徴と見比べるAndroid側でのUI実装のヒント
PDF
UI表現ライブラリを有効活用して iOSアプリのUIをオシャレにするワザ紹介
PDF
2022年の抱負とここ数年続けてきたインプット
Getting a packet trace
IBM Watson Services for Core ML
Master-Detail App を実装する
部品に切り分けて考えるView構造とライブラリを上手に活用したUI実装
ライブラリやView構造を有効活用して iOSアプリのUIをオシャレにするワザ紹介
デザイナー→Webエンジニア→iOSエンジニアと渡り歩いた僕なりのSwiftとの向き合い方と生かす戦略
できるだけUI系のライブラリを用いないアニメーションを盛り込んだサンプル実装まとめ
Approach of Prototyping for making Application User Interface about iOS
Gulp WordPress @Grand-Frontend-Osaka 2015 Summer
SwiftとReactNativeで似たようなUIを作った際の記録
iOS側のUIの特徴と見比べるAndroid側でのUI実装のヒント
UI表現ライブラリを有効活用して iOSアプリのUIをオシャレにするワザ紹介
2022年の抱負とここ数年続けてきたインプット
Ad

Similar to 独自Documentクラス (20)

PPTX
Keep a diary
PDF
第7回 カジュアル Swift 勉強会 @ 青葉台 オープニング #cswift
PPTX
[BUKURO.swift] WWDC2017
PDF
レガシーフリーOSに必要な要素技術 legacy free os
PDF
keep it real
PDF
iOSでのFramework導入のおさらい #関モバ
PDF
SwiftによるiOS開発再入門
PPTX
七曜表と備忘録
PPTX
iOSでライブラリを提供する際に気をつけたいこと
PPTX
Visual Studio Codeを使い倒そう! ~プログラミングから機械学習、クラウド連携、遠隔ペアプロまで~
PDF
Meteor勉強会発表資料「MeteorでiOSアプリを作ろう!」
PPTX
日記アプリでCloud kitを使う
PPTX
使い倒そう Visual Studio Code! ~クラウド連携や遠隔ペアプロ、  もちろん Git も便利に~
PDF
SwiftでAndroidアプリ書けるってよ
PDF
IoTアプリ/ロボット開発をリアルタイムOSでレベルアップしませんか? ~高品質な組込み向けオープンソースを開発するTOPPERSプロジェクトのご紹介~
PPTX
Visual Studio Codeを使い倒そう! ~プログラミングから機械学習、クラウド連携、遠隔ペアプロまで~
PDF
さくらのひやおろし
PDF
Api Meetup #9 - JAZUG
PDF
SwiftとCocoaPodsで始めるサクサクiOS開発!
PDF
Visual Studio Code のこれまでとこれから at OSC 2021 Online/Spring
Keep a diary
第7回 カジュアル Swift 勉強会 @ 青葉台 オープニング #cswift
[BUKURO.swift] WWDC2017
レガシーフリーOSに必要な要素技術 legacy free os
keep it real
iOSでのFramework導入のおさらい #関モバ
SwiftによるiOS開発再入門
七曜表と備忘録
iOSでライブラリを提供する際に気をつけたいこと
Visual Studio Codeを使い倒そう! ~プログラミングから機械学習、クラウド連携、遠隔ペアプロまで~
Meteor勉強会発表資料「MeteorでiOSアプリを作ろう!」
日記アプリでCloud kitを使う
使い倒そう Visual Studio Code! ~クラウド連携や遠隔ペアプロ、  もちろん Git も便利に~
SwiftでAndroidアプリ書けるってよ
IoTアプリ/ロボット開発をリアルタイムOSでレベルアップしませんか? ~高品質な組込み向けオープンソースを開発するTOPPERSプロジェクトのご紹介~
Visual Studio Codeを使い倒そう! ~プログラミングから機械学習、クラウド連携、遠隔ペアプロまで~
さくらのひやおろし
Api Meetup #9 - JAZUG
SwiftとCocoaPodsで始めるサクサクiOS開発!
Visual Studio Code のこれまでとこれから at OSC 2021 Online/Spring
Ad

More from 幸雄 村上 (16)

PDF
アプリケーション識別子.pdf
PDF
圧縮ネイティブ・ライブラリについて.pdf
PDF
分散環境におけるジャストインタイム設定の試み
PDF
SwiftのOptionalを理解する
PDF
え!それって参照渡し?
PDF
プライバシーとセキュリティ(リモート通知のデバイストークンの扱いなど)
PDF
AppleScriptなど
PDF
MojaveのDark Mode
PDF
AppleScriptとは何ぞや
PDF
Web API 通信の符号化について
PPTX
SwiftのOptionalを理解する
PPTX
Swiftでブロックチェーンを実装する
PPTX
OpenGLプログラミング
PPTX
神経回路網の計算
PPTX
日記アプリのデータ管理
PPTX
Cloud kitを使った日記アプリで写真をどう扱うか
アプリケーション識別子.pdf
圧縮ネイティブ・ライブラリについて.pdf
分散環境におけるジャストインタイム設定の試み
SwiftのOptionalを理解する
え!それって参照渡し?
プライバシーとセキュリティ(リモート通知のデバイストークンの扱いなど)
AppleScriptなど
MojaveのDark Mode
AppleScriptとは何ぞや
Web API 通信の符号化について
SwiftのOptionalを理解する
Swiftでブロックチェーンを実装する
OpenGLプログラミング
神経回路網の計算
日記アプリのデータ管理
Cloud kitを使った日記アプリで写真をどう扱うか

独自Documentクラス

  • 1. 独自Documentクラス Cocoa勉強会 関東 Swift勉強会 2018-03 2018/3/16 Bitz Co., Ltd. 村上幸雄
  • 2. • 埼玉県朝霞市でソフトハウスを起業。 • macOSやiOS、Androidのアプリケーション開発を主に請け負う。 • 自社アプリの製作。 • Twitter: @m_yukio • Facebook: yukio.murakami • GitHub: murakami • http://www.bitz.co.jp/weblog/
  • 3. BUKURO.swift • macOS/iOS/watchOS/tvOSプログラマーのための勉強会。 初心者大歓迎。 • Cocoa勉強会 関東とMOSAの合同勉強会。 ほぼ毎月、池袋コワーキングスペース OpenOffice FOREST で開催。 • 申し込みは、connpassから。 https://cocoa-kanto.connpass.com/ • プログラマーが楽しくプログラミングできるようにサポートする場を 提供したい • 勉強会に参加された方全員が学びを得られる様にサポートします! • 勉強会に参加できない方にも学び得られるよう、勉強会の成果を可 能な限りアウトプットします! • プログラマの拠り所となる場を目指します!
  • 4. • PubHack.swiftは、飲み会イベントを通じて、 macOS/iOS/tvOS/watchOS/Androidエンジニアの人たちの交流を目的として います。 • Cocoa勉強会 関東とMOSAの合同イベント。 • 申し込みは、connpassから。 https://cocoa-kanto.connpass.com/
  • 5. Controller View Model Traditional version of MVC as a compound pattern User action Update Update Notify Get changed state Composite Strategy Observer
  • 6. Controller View Model Cocoa version of MVC as a compound design pattern User action Update Update Notify Command Composite Mediator Strategy Observer
  • 7. NSViewControllerNSView View Controller Model NSDocument data NSApplicationDelegate ユーザー操作 event 更新 更新 更新 参照 通知 NSController
  • 8. • iOS 5 からUIDocumentクラスが用意されたが、少々、オ ーバースペック。 • 発表者は、以前から、独自のDocumentクラスを用意して いた。
  • 10. class Document { var version: String private var _uniqueIdentifier: String var uniqueIdentifier: String { return _uniqueIdentifier } static let sharedInstance: Document = { let instance = Document() return instance }() init() { let infoDictionary = Bundle.main.infoDictionary! as Dictionary self.version = infoDictionary["CFBundleShortVersionString"]! as! String self._uniqueIdentifier = "" } deinit { } func load() { loadDefaults() } func save() { updateDefaults() } private func clearDefaults() { let userDefaults = UserDefaults.standard if userDefaults.object(forKey: "version") != nil { userDefaults.removeObject(forKey: "version") } if userDefaults.object(forKey: "uniqueIdentifier") != nil { userDefaults.removeObject(forKey: "uniqueIdentifier") } } private func updateDefaults() { let userDefaults = UserDefaults.standard var versionString: String = "" if userDefaults.object(forKey: "version") != nil { versionString = userDefaults.object(forKey: "version") as! String } if versionString.compare(self.version) != .orderedSame { userDefaults.setValue(self.version, forKey: "version") userDefaults.synchronize() } var uniqueIdentifier: String = "" if userDefaults.object(forKey: "uniqueIdentifier") != nil { uniqueIdentifier = userDefaults.object(forKey: "uniqueIdentifier") as! String } if uniqueIdentifier.compare(self.uniqueIdentifier) != .orderedSame { userDefaults.setValue(self.uniqueIdentifier, forKey: "uniqueIdentifier") userDefaults.synchronize() } } private func loadDefaults() { let userDefaults = UserDefaults.standard var versionString: String = "" if userDefaults.object(forKey: "version") != nil { versionString = userDefaults.object(forKey: "version") as! String } if versionString.compare(self.version) != .orderedSame { /* バージョン不一致対応 */ clearDefaults() _uniqueIdentifier = UUID.init().uuidString } else { /* 読み出し */ if userDefaults.object(forKey: "uniqueIdentifier") != nil { _uniqueIdentifier = userDefaults.object(forKey: "uniqueIdentifier") as! String } } } private func modelDir() -> String { let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true) if paths.count < 1 { return "" } var path = paths[0] path = path.appending(".model") return path } private func modelPath() -> String { let path = modelDir().appending("/model.dat") return path; } }
  • 11. シングルトンを使わない例 @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { public let document = Document() : } let delegate = UIApplication.shared.delegate as! AppDelegate let document = delegate.document print(document)