Last active
September 25, 2018 11:26
-
-
Save ktanaka117/9a3c0a95a7d2b9e8e42dff84f73de810 to your computer and use it in GitHub Desktop.
Swift 4.2におけるIUOの変更
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
// まとめ | |
// ①ImplicitlyUnwrappedOptional<T>で宣言された変数に、Tに "as!" でダウンキャストした変数を代入しようとすると、「それはnilにはならない(はず)」と警告が出る | |
// ②IUOで宣言した変数にはnilが入ることを前提とした上で、利用側でnilが入らない挙動を期待する場合は!をつけるように要求する変更 | |
// ① | |
class ViewController {} | |
class PageViewController: ViewController {} | |
var pageViewController: PageViewController! = PageViewController() | |
var viewController: ViewController = PageViewController() | |
pageViewController = viewController as! PageViewController // <- Treating a forced downcast to 'PageViewController' as optional will never produce 'nil' | |
//let pageVC: PageViewController = viewController as! PageViewController | |
//pageViewController = pageVC | |
//pageViewController = (viewController as? PageViewController)! | |
// ② | |
enum SomeType { case a, b } | |
var someType: SomeType! = .a | |
switch someType { | |
case .a?: | |
() | |
case .b?: | |
() | |
default: | |
() | |
} | |
switch someType! { | |
case .a: | |
() | |
case .b: | |
() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment