- enum 是值类型, 基本语法
enum Direction {
case North
case East
case West
case South
}
let north = Direction.North
let curDirection:Direction = .South
- enum的使用
//枚举可以简写
enum Season {
case Spring,Summer,Autumn,Winter
}
enum Month {
case January,Fabruary,March,April,May,June,July,August,September,October,November,December
}
func getSeason(month:Month) -> Season {
switch month {
case .March, .April, .May:
return Season.Spring
case .June, .July, .August:
return Season.Summer
case .September, .October, .November:
return Season.Autumn
case .December, .January, .Fabruary:
return Season.Winter
}
}
- 原始值(Raw Value)和关联值(Associate Value),原始值和关联值是互斥的,如果设置了关联值就不能设置原始值
//原始值
enum Season:String {
case Spring = "spring"
case Summer = "summber"
case Autumn = "autumn"
case Winter = "winter"
}
Season.Spring.rawValue
if let s = Season(rawValue: "spring"){
print(s)
}
//关联值
enum ATMStatus {
case Success(Int)
case Fail(String)
}
var banlace = 10000
func withDraw(amount : Int) -> ATMStatus {
if banlace >= amount {
banlace -= amount
return .Success(banlace)
}else{
return .Fail("Balance not enough")
}
}
withDraw(900)
- enum 的关联值可以是一个元组
enum Shape{
case Square(side:Double)
case Rectagele(width:Double,height:Double)
case Circle(centerX:Double,centerY:Double,radius:Double)
case Point
}
let square = Shape.Square(side: 100)
let rectagle = Shape.Rectagele(width: 100, height: 300)
let circle = Shape.Circle(centerX: 100, centerY: 20, radius: 56)
func area(shape:Shape) -> Double {
switch shape {
case let .Square(side):
return side*side
case let .Rectagele(width,height):
return width*height
case let .Circle( _ , _ ,radius):
return M_PI*radius*radius
case .Point:
return 0
}
}
area(square)
- 可选型 Optional 本质上是一个enum
var website:Optional<String> = Optional.Some("taobao.com")
enum 的递归定义,使用一个 “indirect” 关键字
struct --是值类型,先来一个例子
struct Location {
var x:Double = 100.0
var y:Double = 100.0
init(){}
init(stringPoint:String){
let xy = stringPoint.characters.split(",")
self.x = atof(String(xy.first!))
self.y = atof(String(xy.last!))
}
init(x:Double,y:Double){
self.x = x
self.y = y
}
mutating func moveHorizental( dist : Double ){
self.x += dist
}
}
var pointA = Location(x : 100, y : 200)
var pointB = Location(stringPoint:"130,232")
var pointC = Location()
pointA.x
pointA.y
pointA.moveHorizental(100)
extension Location{
mutating func moveVertical(dist:Double){
return self.y += dist
}
}
pointB.moveVertical(100)
pointB
extension String {
func isEven() -> Bool{
return self.characters.count % 2 == 0
}
}
"Hello Swift".isEven()
- 计算型属性:依赖其他属性存在的属性
struct Point {
var x = 0.0
var y = 0.0
}
struct Size {
var width = 0.0
var height = 0.0
}
class Rectangle {
var origin = Point()
var size = Size()
//这里的矩形中心点的坐标center依赖origin和size 是一个计算型属性,必须声明称var,必须显式声明类型(Point)
var center:Point{
//getter
get{
let centerX = (origin.x + size.width) * 0.5
let centerY = (origin.y + size.height) * 0.5
return Point(x: centerX, y: centerY)
}
//setter
// set(newCenter){
// origin.x = (newCenter.x - size.width) * 0.5
// origin.y = (newCenter.y - size.height) * 0.5
// }
//setter 可以用newValue,省略掉newCenter参数
set{
origin.x = (newValue.x - size.width) * 0.5
origin.y = (newValue.y - size.height) * 0.5
}
}
//面积 计算型属性
var area:Double{
//只有getter
return size.width * size.height
}
init( origin:Point,size:Size){
self.origin = origin
self.size = size
}
}
- 类型属性:type property,定义在类型上的属性,例如 Int.min,使用static 修饰
- 属性观察器 property observer ,使用willSet 和didSet ,注意,willSet和didSet 在类初始化的时候不会调用