Created
August 5, 2015 09:51
-
-
Save syou007/7ab44f019ff89212c78b to your computer and use it in GitHub Desktop.
Swiftのアプリ設計時に決めることメモ ref: http://qiita.com/syou007/items/85e18a7f3555edd96946
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class API { | |
// APIを使用するか? | |
static let USE_FLG = true | |
// class let USE_FLG = true // この定義でも可能で好みで使えば良いと思う。僕はstaticを好む。 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class API { | |
// Public変数 | |
var publicFlg = true | |
// Private変数 | |
private var _privateFlg = false | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
switch userName { | |
case .Hero | |
// 主人公だった場合の処理 | |
case .King | |
// 王様だった場合の処理 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if userName == .King { // ←ここの.Kingがコード補完されない。userNameの型は決まってるから補完されてもいいはずなのに。 | |
// 王様の処理 | |
} else if userName == .Hero { | |
// 主人公の処理 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if userName == UserName.King { // ←ここだけ型省略で書くとエラーになるケースがある。(型推論されない時がある・・・?) | |
// 王様の処理 | |
} else if userName == .Hero { // ←こっちは平気。 | |
// 主人公の処理 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DataCenter | |
∟Users(Array) | |
∟User(NSCodingインターフェースを実装したオブジェクト) | |
∟User | |
∟Items(Array) | |
∟Item | |
∟Item |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func api(callback:(result:Result)->()) { | |
// API呼び出し処理 | |
... | |
// 非同期で結果を返す。 | |
callback(.OK) | |
} | |
// API呼び出し | |
api() { result in | |
// 結果を受けとった後の処理 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// プロトコル(インターフェース)を事前に決めておく必要がある。 | |
protocol ApiDelegate { | |
func callback(result:Result) -> () | |
} | |
// レシーバーではプロトコルを実装しておく必要がある。 | |
class Controller: ApiDelegate { | |
// APIを呼び出す | |
func main() { | |
let api = API() | |
// ApiDelegateを実装しているクラスを受け渡す。 | |
api.delegate = self | |
api.call() | |
} | |
func callback(result:Result) { | |
// 結果を受け取った時の処理 | |
} | |
} | |
// API呼び出し側ではdelegateを受け取る必要がある。 | |
class Api { | |
// よほどの理由がない限りはweakを必ず付けること | |
weak var delegate:ApiDelegate? | |
// API呼び出し処理 | |
func call() { | |
// 非同期で結果を返します。 | |
self.delegate?(.OK) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// よほどの理由がない限りはweakを必ず付けること | |
weak var delegate:ApiDelegate? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Controller { | |
// 初期化処理 | |
init() { | |
// コールバック先を指定します。 | |
NSNotificationCenter.defaultCenter().addObserver(self, selector: "callback:", name: "apiCall", object: nil) | |
} | |
// APIを呼び出す | |
func main() { | |
// コールバック先は既に設定済み | |
Api.call() | |
} | |
func callback(notification: NSNotification?) { | |
// 結果を受け取った時の処理 | |
let result = notification.userInfo | |
} | |
deinit() { | |
// 登録したコールバックを削除します。(これを行わないとコールバック処理がどんどん積まれていってしまう。) | |
NSNotificationCenter.defaultCenter().removeObserver(self) | |
} | |
} | |
// API呼び出しクラス | |
class Api { | |
// API呼び出し処理 | |
class func call() { | |
// 非同期で結果を返します。 | |
NSNotificationCenter.defaultCenter().postNotificationName("apiCall", object: nil, userInfo: ["result": Result.OK]) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// init処理では入れれないが必ずデータが入る場合。 | |
// (viewWillAppear、viewDidLoadなどで初期データを設定する場合) | |
var apiAccessor:ApiAccessor()! | |
// Xibなどの外部ツールからオブジェクトが挿入される時(後述) | |
@IBOutlet private weak var _nameText:UITextField! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 親Viewを保持する。 | |
weak var parentView:UIView? | |
// ApiAccessorがなんらかの事情で取得できない場合はAPI通信の処理を行わない。 | |
weak var apiAccessor:ApiAccessor()? | |
// チェックボックスの初期値を設定する。ただし、値が入ってない場合はチェックボックス自体を表示しない。 | |
// あまり良い例ではないが、こういう使い方をするとBool型で三つの分岐を作れる。 | |
var checkBoxFlg:Bool? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let value:Int | |
if randFlg { | |
value = 1 | |
} else { | |
value = 0 | |
} | |
println("\(value)") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@IBOutlet private weak var _nameText:UITextField! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
weak var _nameText:CCLabelTTF! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SaveData: NSObject { | |
// クリティカルなアプリでない場合はこの程度で十分。 | |
static let sharedInstance = SaveData() | |
} | |
// 保存するときにもこんな感じでできる。(ただしsharedInstanceは長い名前なので僕はdataなどの短いメソッド名で操作している。) | |
SaveData.sharedInstance().save() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum UserName:String { | |
case Hero = "主人公" | |
case King = "王様" | |
} | |
println("\(UserName.Hero)はこう言った。") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if UserName(rawValue: "入力値") == nil { | |
// ゲームでは使ってない名前 | |
} else { | |
// ゲームで使ってる名前 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment