class struct enum
enum
枚舉是為一組有限種可能性的相關(guān)值提供的通用類(lèi)型,關(guān)鍵詞enum來(lái)定義枚舉,在一對(duì)大括號(hào)內(nèi)定義具體內(nèi)容包括使用case關(guān)鍵字列舉成員
枚舉中有兩個(gè)概念:原始值(raw value)、關(guān)聯(lián)值(associated value),
1.枚舉的原始值
枚舉成員可以用相同類(lèi)型的默認(rèn)值預(yù)先填充,這樣的值就是原始值(raw value),Int修飾的是FruitType成員原始值的類(lèi)型而不是FruitType的類(lèi)型
enum FruitType: Int{
case apple = 10
case orange = 11
case banana = 15
}
可以使用枚舉成員的rawValue屬性來(lái)訪問(wèn)成員的原始值,或者是使用原始值初始化器來(lái)嘗試創(chuàng)建一個(gè)枚舉實(shí)例
2.枚舉的關(guān)聯(lián)值(associated value)
關(guān)聯(lián)值是為枚舉的成員們綁定了一組類(lèi)型,不同的成員可以是不同的類(lèi)型(提供關(guān)聯(lián)值時(shí)用的是括號(hào))
enum FruitType{
case apple (String)
case orange (Int,String)
case banana (Int,String)
}
var apple = FruitType.apple("app")
var orange = FruitType.orange(5, "org")
var banana = FruitType.banana(10, "ban")
switch banana {
case .apple(let value):
print(value)
case .banana(let num,let name):
print("\(name) has \(num)")
default:
print("default")
}
let apple = FruitType. apple.rawValue
print(apple)//10
let orange = FruitType.init(rawValue: 11)
print(orange!)//orange
let ban = FruitType.init(rawValue: 16)
if ban == nil{
print("nil")
}
枚舉、結(jié)構(gòu)體、類(lèi)的共同點(diǎn):
1,定義屬性和方法;
2,下標(biāo)語(yǔ)法訪問(wèn)值;
3,初始化器;
4,支持?jǐn)U展增加功能;
5,可以遵循協(xié)議;
二.不同點(diǎn)
類(lèi)具有結(jié)構(gòu)體不具有的功能如下:
類(lèi)特有的功能:
1,繼承;
2,允許類(lèi)型轉(zhuǎn)換;
3,析構(gòu)方法釋放資源;
4,引用計(jì)數(shù);
類(lèi)和結(jié)構(gòu)體區(qū)別
1.定義結(jié)構(gòu)體類(lèi)型時(shí)其成員可以沒(méi)有初始值。定義一個(gè)類(lèi)沒(méi)有初始值,編譯器是會(huì)報(bào)錯(cuò)的,他會(huì)提醒這個(gè)類(lèi)沒(méi)有被初始化。
2.定義結(jié)構(gòu)體類(lèi)型時(shí)其成員可以沒(méi)有初始值,但是創(chuàng)建結(jié)構(gòu)體實(shí)例時(shí)該實(shí)例的成員必須有初值。
3.在結(jié)構(gòu)體內(nèi)部方法中如果修改了結(jié)構(gòu)體的成員,那么該方法之前應(yīng)該加入:mutating關(guān)鍵字,結(jié)構(gòu)傳遞 是作為值傳遞 ,所以要inout處理
4.class是引用類(lèi)型 strcut 是值類(lèi)型
struct Student {
var chinese: Int = 50
var math: Int = 50
var english: Int = 50
//修改數(shù)學(xué)成績(jī)
mutating func changeMath(num: Int) {
self.math += num
}
}
var student = Student(chinese: 20, math: 30, english: 40)
//更改結(jié)構(gòu)體屬性值
//方法1
//更改分?jǐn)?shù)中語(yǔ)文學(xué)科的成績(jī)
func changeChinese(num: Int, student: inout Student){
student.chinese += num
}
changeChinese(num: 20, student: &student)
print(student.chinese,student.math)
//方法2
student.changeMath(num: 10)
class是引用類(lèi)型 strcut 是值類(lèi)型
struct SRect {
var width = 100
var height = 100
}
class CRect {
var width = 100
var height = 100
}
var sRect = SRect(width: 150, height: 150)
print("struct===\(sRect.width)==\(sRect.height)")
var cRect = CRect()
print("class===\(cRect.width)==\(cRect.height)")
var newSrect = sRect
print(newSrect.width,newSrect.height)
newSrect.width = 200
newSrect.height = 200
print("struct是值類(lèi)型")
print("newSrect===\(newSrect.width)==\(newSrect.height)")
print("srect===\(sRect.width)==\(sRect.height)")
var newCRect = cRect
newCRect.width = 300
newCRect.height = 300
print("class是引用類(lèi)型")
print("newCRect===\(newCRect.width)==\(newCRect.height)")
print("crect===\(cRect.width)==\(cRect.height)")
輸出結(jié)果:
struct===150==150
class===100==100
150 150
struct是值類(lèi)型
newSrect===200==200
srect===150==150
class是引用類(lèi)型
newCRect===300==300
crect===300==300