Optionals

Optionals

var name = "Matt Galloway"
var age = 30
var occupation = "Software Developer & Author"

var errorCode: Int?
errorCode = 100
errorCode = nil

var result: Int? = 30
// print(result) // warning
//print(result + 1) // error: Value of optional type 'Int?' not unwrapped; did you mean to use '!' or '?'?
print(result ?? 0)
print(result!)
print(result as Any)

// IF-LET BINDING (AND FORCED UNWRAPPING)
var authorName: String? = "Matt Galloway"
var authorAge: Int? = 30

var unwrappedAuthorName = authorName!
print("Author is (unwrappedAuthorName)")

authorName = nil
//print("Author is (authorName!)")

if authorName != nil {
print("Author is (authorName!)")
} else {
print("No author.")
}

if let unwrappedAuthorName = authorName {
print("Author is (unwrappedAuthorName)")
} else {
print("No author.")
}

if let authorName = authorName {
print("Author is (authorName)")
} else {
print("No author.")
}

if let authorName = authorName, let authorAge = authorAge {
print("The author is (authorName) who is (authorAge) years old.")
} else {
print("No author or no age.")
}

if let authorName = authorName, let authorAge = authorAge, authorAge >= 40 {
print("The author is (authorName) who is (authorAge) years old.")
} else {
print("No author or no age or age less than 40.")
}

// GUARD
func calculateNumberOfSides(shape: String) -> Int? {
switch shape {
case "Triangle":
return 3
case "Square":
return 4
case "Rectangle":
return 4
case "Pentagon":
return 5
case "Hexagon":
return 6
default:
return nil
}
}

func maybePrintSides(shape: String) {
guard let sides = calculateNumberOfSides(shape: shape) else {
print("I don't know the number of sides for (shape).")
return
}

print("A (shape) has (sides) sides.")
}

// NIL COALESCING
var optionalInt: Int? = 10
var mustHaveResult = optionalInt ?? 0

optionalInt = nil
mustHaveResult = optionalInt ?? 0

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容