Upload
hiromichi-yamada
View
273
Download
0
Embed Size (px)
Citation preview
デリゲートを理解して 脱初心者!
株式会社トルクス 山田 2015/07/15 iOSアプリ勉強会
Torques Inc. 2015
自己紹介:山田 宏道• 株式会社トルクス 代表
Torques Inc. 2015
VRコンテンツ• 「MotherBird」ニコニコ超会議(オッキューランド)
Torques Inc. 2015
ときどき、講師
Torques Inc. 2015
今日は、デリゲート• iOSアプリ開発でつまづくポイント
Torques Inc. 2015
開発で重要な4要素• Tools(Xcode, Android Studio, Eclipse) • Language(Swift, Obj-C, Java, PHP, ..) • Libraries, Frameworks(iOS SDK, etc) • Algorithms, Design Patterns(←ココ!)
Torques Inc. 2015
デザインパターンとは<クラスのつくりかたの事例集> • GoF(The Gang of Four)が有名だが、全部知っておく必要はない
• Delegation Patternというやり方があり、ある仕組みを作るのに便利、となっている。
Torques Inc. 2015
クラス、メソッドの復習
Torques Inc. 2015
クラス、メソッドの復習class Human { var name: String = "Default" var stamina: Int = 100 var pos: CGPoint = CGPoint(x: 0, y: 0) init( name: String ) { self.name = name } func sayHello() { println("My name is \(name)! @ (\(pos.x),\(pos.y))“) } }
// -----
var me = Human(name:"Yamada") me.sayHello()
Torques Inc. 2015
SwiftのProtocol
「プロトコルに準拠している」 =「必要な条件を満たしている」
=「必要なメソッドを実装している」
Torques Inc. 2015
SwiftのProtocolprotocol PlayerProtocol { func runTo( target:CGPoint ) }
class Player: Human, PlayerProtocol { }
Torques Inc. 2015
SwiftのProtocolprotocol PlayerProtocol { func runTo( target:CGPoint ) }
class Player: Human, PlayerProtocol { }
<実装デモ>
Torques Inc. 2015
SwiftのProtocolprotocol PlayerProtocol { func runTo( target:CGPoint ) }
class Player: Human, PlayerProtocol { func runTo( target:CGPoint ) { println("ε=ε=ε=┌(;*´Д`)ノ") } }
Torques Inc. 2015
SwiftのProtocolvar players = [Player(name: "P0"),Player(name: "P1"),Player(name: "P2"),Human(name: "Hiro")]
var ball = CGPoint(x: 10, y: 10)
for p in players { p.sayHello() if let pr = p as? PlayerProtocol { pr.runTo(ball) } }
Torques Inc. 2015
SwiftのProtocol
「プロトコルに準拠している」 =「必要な条件を満たしている」
=「必要なメソッドを実装している」
Torques Inc. 2015
delegate
「任せられる人に、任せる」 =「必要な実装をしているオブジェクトに、任せる」 =「プロトコルに準拠しているオブジェクトに、任せる」
Torques Inc. 2015
パルプンテDelegateprotocol ParupunteProtocol { func performParupunte() }
class GameSystem { }
class Hero : Human { func sayParupunte() { } }
// ----- var game = GameSystem() var hero = Hero(name: "ああああ") hero.sayParupunte()
Torques Inc. 2015
パルプンテDelegateprotocol ParupunteProtocol { func performParupunte() }
class GameSystem: ParupunteProtocol { func performParupunte(){ println("パルプンテ!") } }
Torques Inc. 2015
パルプンテDelegateclass Hero : Human { var parupunteDelegate: ParupunteProtocol? func sayParupunte() { if( parupunteDelegate != nil ){ parupunteDelegate.performParupunte() } } }
// ----- var game = GameSystem() var hero = Hero(name: “ああああ") hero.parupunteDelegate = game hero.sayParupunte()
Torques Inc. 2015
パルプンテDelegate:全体protocol ParupunteProtocol { func performParupunte() }
class GameSystem: ParupunteProtocol { func performParupunte(){ println("パルプンテ!") } }
class Hero : Human { var parupunteDelegate: ParupunteProtocol? func sayParupunte() { if( parupunteDelegate != nil ){ parupunteDelegate.performParupunte() } } }
// ----- var game = GameSystem() var hero = Hero(name: “ああああ") hero.parupunteDelegate = game hero.sayParupunte()
Torques Inc. 2015
AvatarMaker• 実践的な問題をご用意しました! • github.com/HiromichiYamada/AvatarMakerProblem
• ↑”AvatarMaker”で検索してみてください
Torques Inc. 2015
AvatarMaker• MainViewController • WearPickerViewController
Torques Inc. 2015
AvatarMaker• 実装例:デモ
Torques Inc. 2015
ありがとうございました• facebook hiromichi.yamada • 他に、詳しく知りたい技術とかあったらアンケート等にお願いします。