Created
December 15, 2014 16:02
-
-
Save reizist/6089bfb672468a1e8a7d to your computer and use it in GitHub Desktop.
Optional型の理解
This file contains hidden or 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
// Playground - noun: a place where people can play | |
import UIKit | |
var str: String = "hoge" | |
// str = nil => NilLiteralConvertiable: 通常の型にnilは代入できない | |
var str2: Optional<String> = "hoge" // str2: String? = "hoge"と等しい | |
// str2 = nil => ok | |
// str2.upperCaseString => error: 'Optional<String>' does not have a member named | |
str2!.uppercaseString // Forced Unwrapping | |
str2?.uppercaseString // Optional Chaining: 結果もOptional型 | |
if let unwrapped: String = str2 { | |
unwrapped.uppercaseString // Optional Binding: nilでなければString型変数に格納する | |
} | |
var str3: ImplicitlyUnwrappedOptional<String> = "hoge" // str3: String! = "hoge"のシンタックスシュガー | |
str3.uppercaseString // ImplicitlyUnwrappedOptional型で宣言すると使用時に自動でForced Unwrapping | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment