通过 GuidedTour 学习 swift
learning swift by GuidedTour
在 Swift 的 GuidedTour 示例中,作者是
https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/
var optionalString: String? = "Hello"
optionalString == nil
var optionalName: String? = "John Appleseed"
var greeting = "Hello!"
if let name = optionalName {
greeting = "Hello, \( name)"
}
我不能做这个实验,因为错误
将 optionalName
更改为 nil
。你得到什么问候?添加一个 else
子句,如果 optionalName
是 nil
.
,则设置不同的问候语
当我创建新的 playground 并输入与您 post 完全相同的代码时,它起作用了。你能 post 完整的代码吗?
// Playground - noun: a place where people can play
import Foundation
var optionalString: String? = "Hello"
optionalString == nil
var optionalName: String? = "John Appleseed"
var greeting = "Hello!"
if let name = optionalName {
greeting = "Hello, \( name)"
}
唯一不同的是,您似乎忘记导入 Foundation 框架:
import Foundation
var optionalString: String? = "Hello"
print(optionalString == nil)
var optionalName: String? = nil
var greeting = "Hello!"
if let name = optionalName {
greeting = "Hello, \(name)"
}else{
greeting = "Hello Max"
}
greeting
在 Swift 的 GuidedTour 示例中,作者是
https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/
var optionalString: String? = "Hello"
optionalString == nil
var optionalName: String? = "John Appleseed"
var greeting = "Hello!"
if let name = optionalName {
greeting = "Hello, \( name)"
}
我不能做这个实验,因为错误
将 optionalName
更改为 nil
。你得到什么问候?添加一个 else
子句,如果 optionalName
是 nil
.
当我创建新的 playground 并输入与您 post 完全相同的代码时,它起作用了。你能 post 完整的代码吗?
// Playground - noun: a place where people can play
import Foundation
var optionalString: String? = "Hello"
optionalString == nil
var optionalName: String? = "John Appleseed"
var greeting = "Hello!"
if let name = optionalName {
greeting = "Hello, \( name)"
}
唯一不同的是,您似乎忘记导入 Foundation 框架:
import Foundation
var optionalString: String? = "Hello"
print(optionalString == nil)
var optionalName: String? = nil
var greeting = "Hello!"
if let name = optionalName {
greeting = "Hello, \(name)"
}else{
greeting = "Hello Max"
}
greeting