Created
January 20, 2016 17:26
-
-
Save SatoTakeshiX/5e15e8bcfbe6a30cc80e to your computer and use it in GitHub Desktop.
オプショナル型のサンプル
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
//: Playground - noun: a place where people can play | |
import UIKit | |
var strs : String? = "SOMESTRING" | |
print(strs!.lowercaseString) | |
var a: Int? | |
//var b: Optional<Int> // Int? と同じ意味 | |
a = nil //->nilが入っても大丈夫 | |
var b: Int // 非 optional 型 | |
//b = nil // -> Nil cannot be assigned to type 'Int' | |
var unoptionalInt: Int = 1 | |
print(unoptionalInt + 2) // -> 3 | |
var optionalInt : Int? = 1 | |
print(optionalInt! + 2) //Value of optional type 'Int?' not unwrapped; | |
//optionalInt = nil | |
//print(optionalInt!) | |
var wrappedInt: Int? = 1 // Optional 型 | |
var optionalString : String? = "SOME_STRING" | |
print(optionalString?.lowercaseString) | |
/*オプショナルチェーイング | |
?を使ってつなげる。つなげるなかでnilがあったらそこですべてnilになる | |
optional型が変える | |
*/ | |
class Room { | |
var adress : String = "渋谷区" | |
} | |
class Person { | |
var room : Room? | |
} | |
var aperson = Person() | |
aperson.room?.adress = "目黒区" | |
aperson.room = Room() | |
print(aperson.room?.adress) | |
if let adress = aperson.room?.adress{ | |
print(adress) | |
}else{ | |
print("住所が登録されていません") | |
} | |
while let adress = aperson.room?.adress{ | |
print(adress) | |
break | |
} | |
// | |
// 自動的にアンラップされる | |
// | |
print(wrappedInt == 1) // -> true | |
print(wrappedInt >= 1) // -> true | |
print(wrappedInt > 1) // -> false | |
print(wrappedInt <= 1) // -> true | |
print(wrappedInt < 1) // -> false | |
print(wrappedInt != 1) // -> false | |
wrappedInt = nil | |
print(wrappedInt == 1) // -> false | |
var str = "Hello, playground" | |
let possibleNumber = "123" | |
let convertedNumber = Int(possibleNumber) | |
print(convertedNumber) | |
var serverResponseCode: Int? = 404 | |
// serverResponseCode contains an actual Int value of 404 | |
serverResponseCode = nil | |
// serverResponseCode now contains no value” | |
if convertedNumber != nil { | |
print("convertedNumber has an integer value of \(convertedNumber!).") | |
} | |
// prints "convertedNumber has an integer value of 123.” | |
/* | |
オプショナルバインディング | |
*/ | |
if let actualNumber = Int(possibleNumber) { | |
print("\'\(possibleNumber)\' has an integer value of \(actualNumber)") | |
} else { | |
print("\'\(possibleNumber)\' could not be converted to an integer") | |
} | |
//actualNumberは初期化されているので!でアンラップしなくてもよい | |
if let firstNumber = Int("4"), secondNumber = Int("42") where firstNumber < secondNumber { | |
print("\(firstNumber) < \(secondNumber)") | |
} | |
// prints "4 < 42” | |
let possibleString: String? = "An optional string." | |
let forcedString: String = possibleString! // requires an exclamation mark | |
print(forcedString) | |
/**implicitly unwrapped optional*/ | |
var assumedString: String! = "An implicitly unwrapped optional string." | |
let implicitString: String = assumedString // no need for an exclamation mark” | |
print(implicitString) | |
/*オプショナルバインディングもできる*/ | |
if let definiteString = assumedString { | |
print(definiteString) | |
} | |
assumedString = nil | |
//print(assumedString) ->implicitly unwrapped optionalにnilいれるてアクセスするとランタイムエラーになる |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment