通过GuidedTour快速学习

时间:2015-01-28 17:10:38

标签: swift-playground

在这个GuidedTour of Swift的例子中

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。你有什么问候?如果elseoptionalName,请添加nil子句设置不同的问候语。

2 个答案:

答案 0 :(得分:0)

当我创建新游乐场并输入与您发布的完全相同的代码时,它可以正常工作。你能发布完整的代码吗?

// 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

答案 1 :(得分:0)

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