import Foundation
/**
* 1,第一个swift程序
*/
println("Hello, World!")
/**
* 2,定义常量
*/
let a =10
let cInteger:Double =12.00
/**
* 3,定义变量
*/
var b =0
b =11
var bFloat:Float
bFloat = 11.0
println(a)
/**
* 4,类型转换(字符串的拼接)
*/
let label =
"The width is "
let width =94
let widthLabel =label +
String(width)
//字符串的拼接
let widthLabel1 ="\(label)\(width)"
println(widthLabel)
println("类型转换:\(widthLabel1)")
/**
* 5,定义数组
*/
var array = ["hehe","haha","nimei"]
println(array)
println(array[0])
array[0] ="kebe"
println("数组:\(array)")
//定义一个类型不确定的空数组
var array2 = []
array2 = [1,2]
println(array2)
println(array2[1])
/**
* 6,定义字典
*/
var dictionary = ["xiaoming":"10","xiaohong":"8","xiaofang":"12"]
println(dictionary)
println(dictionary["xiaoming"])
dictionary["xiaoming"] ="20"
println(dictionary)
//定义一个类型不确定的空字典
var dict2 = [:]
dict2 = [2:"2",3:3]
println(dict2)
/**
* 7。流程控制
*/
let dataArray = [1,2,3,4,5,6]
var sore =0
for soretin
dataArray{
if soret >
3{
sore +=
5
}else{
sore -=
2
}
}
println(sore)
var count =0;
forvar i =
0;i <3;++i{
count +=
1;
}
println("count=\(count)")
var firstForLoop =0
for iin
1...4 {
firstForLoop += i
}
println("firstForLoop=\(firstForLoop)")
func greet(name:String,day:String)->String{
return"hello,\(name),Today is
\(day)"
}
let look =
greet("kobe","Monday")
println("\(look)")
/**
使用元组(tuple)来返回多个值
*/
func getPrice()->(Double,Int,Float){
return (3.33,3,3.3)
}
//let a = getPrice()
println("\(getPrice())")
/**
* 函数能够接受可变參数,收集到一个数组中
*/
func sumof(numbers:Int...)->Int{
var sum =
0
for number
in numbers{
sum += number
}
return sum
}
let suma =sumof()
let sumb =sumof(12,12,123)
println("suma=\(suma)")
println("sumb =\(sumb)")
/**
* 对象和类
*/
class Shape {
var numberOfSides =
0
func simpleDescriptio()->String{
return"A shape wiht
\(numberOfSides)"
}
}
var shape =Shape()
shape.numberOfSides =7
let shapDes =
shape.simpleDescriptio()
println("shapDes =\(shapDes)")
/**
* 使用init构造器来实例一个类
*/
class Shape1 {
var numberOfSides =
0
var name:String
init(name:String){
self.name = name
}
func simpleDescriptio()->String{
return"A shape wiht
\(numberOfSides)"
}
}
var shape1 =Shape1(name:
"wang")
println("shape1 =\(shape1.name)")