// Playground - noun: a place where people can play
import Cocoa
var str = "Hello, playground"
// 定义常量
let schoolNum:String="罗子山中心小学"
// 定义变量
var age:Int=19
age=28
// 显示转换
var ageFolat:Float=Float(age)
// 数组、字典
var shoppingList=["catfish","water","blue paint"]
shoppingList[1]
var occupations=["1":"water","2":"apple"]
occupations["1"]
occupations["3"]="admin"
occupations["3"]
// 创建空的数组和字典
let emptyArray = [String ]()
let emptyDictionary=Dictionary<String,Float>()
// 循环
let indicdualScores=[75,43,103,89]
var teamScore=0
for score in indicdualScores{
if score>50{
println(score)
}else{
println(score+1)
}
}
// 可空
var optionalString:String?="Hello"
optionalString=nil
var strGreeting:String?="hello"
if let name=strGreeting{
println(name)
}
var ass:String=" 222321"
//switch
let vegetable="Red popper"
switch vegetable{
case "celery":
let vegetableComment="Add Somen"
case "Red","Bule":
let vegetableComment="Update Somen"
case let x where x.hasSuffix("popper") :
let vegetableComment="Delete from"
default :
let vegetableComment="Default"
}
// for in遍历 字典
/*
let interestingNumbers=["1":[1,2,3,4,5],"2":[9,8,7,6,5]]
var largest=0
for(kind,numbers) in interestingNumbers{
for number in numbers{
if number > largest
largest=number
}
}
*/
// while
var n=2
while n<100{
n=n*2
}
println(n)
// ..表示区间
/*
var firstForLoop = 0
for i in 0..3{
firstForLoop += i
}
println(firstForLoop)
*/
for var i=0;i<3; ++i{
println(i)
}
// 方法
func greet(name:String,day:String)->String{
return "hello\(name),Today is\(day)"
}
greet("tony", "1")
func getGasPrices()->(Double,Double,String){
return (3.123,123.2,"tony")
}
getGasPrices()
// 方法参数可以是动态的
func sunOf(numbers:Int...)->Int{
var sum=0
for number in numbers{
sum+=number
}
return sum
}
sunOf()
sunOf(12,32,32)
// 函数可以嵌套
func returnFifteen()->Int{
var y=10
func add(){
y+=5
}
add()
return y
}
returnFifteen()
var aa=123
// 函数可以作为另外一个函数返回值
/*
func makeIncrementer()->(Int->Int){
func addOne(number:Int)->Int{
return 1+number
}
return addOne
}
var increment=makeIncrementer()
increment(8)
*/