Created
February 18, 2016 04:35
-
-
Save ezura/4d9bd1de64d9a1db4bdd to your computer and use it in GitHub Desktop.
Optional じゃない場合でも中身が nil になってる可能性あるよねって思ったの #CodePiece
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 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