Skip to content

Instantly share code, notes, and snippets.

@ezura
Created February 18, 2016 04:35
Show Gist options
  • Save ezura/4d9bd1de64d9a1db4bdd to your computer and use it in GitHub Desktop.
Save ezura/4d9bd1de64d9a1db4bdd to your computer and use it in GitHub Desktop.
Optional じゃない場合でも中身が nil になってる可能性あるよねって思ったの #CodePiece
class ActionDelegate {
let member = "ActionDelegate"
}
class DelegateSampleClass {
weak var delegate : ActionDelegate!
init (delegate: ActionDelegate) {
self.delegate = delegate
}
func notOptional() {
delegate.member
}
func optional() {
delegate?.member
}
}
var delegate = ActionDelegate()
let sample = DelegateSampleClass(delegate: delegate)
sample.optional()
sample.notOptional()
delegate = ActionDelegate() // 新しいのいれて sample に渡した delegate の参照を減らす -> 解放
sample.optional()
sample.notOptional() // atal error: unexpectedly found nil while unwrapping an Optional value
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment