码迷,mamicode.com
首页 > 编程语言 > 详细

Swift-类、结构体、枚举

时间:2018-07-24 20:54:48      阅读:148      评论:0      收藏:0      [点我收藏+]

标签:ast   each   ant   number   ret   arc   整型   Plan   back   

001-枚举

//枚举 swift 中不会被赋于默认的整型值
enum someEnumeration {
    case north
    case sourth
    case east
    case west
}
//多个成员值要写在一行,用逗号隔开
enum Planet {
    case mercury,earth,mars
}
var directionHead = someEnumeration.west
        directionHead = .east
        //使用Switch匹配单个的枚举值
        switch directionHead {
        case .north:
            print("north")
        case .sourth:
            print("sourth")
        case .west:
            print("west")
        case .east:
            print("east")
        default:
            print("None Of This Direction")
        }

//枚举关联值

enum BarCode {
    case upc(Int,Int,Int,Int) //根据数字识别商品
    case qrCode(String)       //根据代码识别商品
}

//创建一个条形码
 var productBar = BarCode.upc(8, 8, 3, 4)
 productBar = .qrCode("ABCDE")//upc被qrCode代替

//枚举原始值

//枚举的原始值 隐士赋值
enum ASCIIControlChar : Character {
    case tab = "\t"
    case lineFeed = "\n"
    case carriageReturn = "\r"
}
//字符串的默认值是枚举值本身
enum Planet:Int{
    case mercury = 1,venus,earth,mars
}
//获取枚举变量的原始值
     let earthOrder = Planet.earth.rawValue
     print(earthOrder)//输出结果为 3

//枚举递归

//枚举递归
indirect enum Arithmetic {
    case number(Int)
    case addition(Arithmetic,Arithmetic)
    case multiplication (Arithmetic,Arithmetic)
}
   //创建一个枚举递归表达式
   let five = Arithmetic.number(5)
   let four = Arithmetic.number(4)
   let sum = Arithmetic.addition(five, four)
   let product = Arithmetic.multiplication(sum, five)
    // (5+4) * 5
    print(product)

//类

//类的声明:
class Student{
    var name:String = ""
    var age:Int = 18
}
//访问属性
   classItem.name = "小子"
   print(classItem.name)//小子

//结构体

//结构体
struct teacher {
    var name:String = ""
    var age:Int = 20
}
let myTeacher = teacher()
 print(myTeacher)//teacher(name: "", age: 20)

 

 

 

 

Swift-类、结构体、枚举

标签:ast   each   ant   number   ret   arc   整型   Plan   back   

原文地址:https://www.cnblogs.com/StevenHuSir/p/Swift_Enum.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!