感嘆符を使用してオプションをアンラップする必要がある場合を知るのに十分なオプションの使用を理解しています。ガードステートメントに感嘆符が必要ないのはなぜですか?
このコードは機能し、コンパイルされますが、感嘆符は使用されません。
struct Blog{
var author:String?
var name: String?
}
func blogInfo2(blog:Blog?){
guard let blog = blog else {
print("Blog is nil")
return
}
guard let author = blog.author, name = blog.name else {
print("Author or name is nil")
return
}
print("BLOG:")
print(" Author: \(author)")
print(" name: \(name)")
}
このコードは、感嘆符を付けた場合にも機能します。
struct Blog{
var author:String?
var name: String?
}
func blogInfo2(blog:Blog?){
guard let blog = blog! else {
print("Blog is nil")
return
}
guard let author = blog.author!, name = blog.name! else {
print("Author or name is nil")
return
}
print("BLOG:")
print(" Author: \(author)")
print(" name: \(name)")
}
これは少し矛盾していませんか、それとも感嘆符が必要ない理由を誰かが明確に説明できますか?