您的位置:首页 > 移动开发 > Swift

[IOS] Swift enum and Struct

2015-08-08 10:00 471 查看
// ——————enum——————————

// type 1
enum Direction{
case East
case South
case West
case North
}
enum Direction2{
case East, South,West, North
}

var d = Direction.East
var d2: Direction = .East
d2 = .South

// type 2 ,  Course enum
enum Course :Int{
case Android = 1,IOS,WP,Symbian
}

var c1 = Course.Android
let k1 = c1.rawValue
let k2 = Course.self.init(rawValue:k1)


//———————struct——————————–

struct Sdemo {
var x:Int ;
var y:Int ;
//Constructor
init(){
x = 0;
y = 0;
}
init(x:Int,y:Int){
self.x = x
self.y = y
}
//用"_",这样就不用写 x:和 y:了
init(_ x:Int, _ y:Int){
self.x = x
self.y = y
}
// function is legal
func getCenter() -> Int{
return (x+y)/2
}
//if you wanna modify the x and y , you should add "mutating"
mutating func addOffSet(deltaX:Int ,deltaY:Int)(){
y += deltaY
x += deltaX
}
}
var s = Sdemo(x:100,y:100);
var s3 = Sdemo(100,100)
var s2 = Sdemo()
s3.addOffSet(100, deltaY: 200)


————-结构体默认带label构造函数——————-

struct Rect{
var origin :Int;
var size :Int;
}
var rect = Rect(origin : 10, size : 10);
//-------------------------------------------------------
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: