1. 下標(biāo)腳本
下標(biāo)腳本是訪問對(duì)象、集合或者序列的快捷方式,開發(fā)者不需要調(diào)用實(shí)例指定的賦值和訪問語法兜看,就可以直接訪問所需的數(shù)值,例如perArray[index]狭瞎、perDictionary[index]
// 現(xiàn)在在類中我們定義一個(gè)小標(biāo)語法细移,用關(guān)鍵字subscript
class NumberOfPeople{
var principalNumber:Int = 0
var teacherNumber:Int = 0
var studentNumber:Int = 0
subscript(index:Int) -> Int{
get{
switch index{
case 0:
return principalNumber
case 1:
return teacherNumber
case 2:
return studentNumber
default:
return 0
}
}
set{
switch index{
case 0:
return principalNumber = newValue
case 1:
return teacherNumber = newValue
case 2:
return studentNumber = newValue
default:
return
}
}
}
}
// 實(shí)例對(duì)象
var personNumber = NumberOfPeople()
// 下角標(biāo)的使用
personNumber[0] = 1
personNumber[1] = 20
personNumber[2] = 100
for i in 0...2{
print(personNumber[i])
}
// 結(jié)果打印 1 20 100
2.自動(dòng)引用計(jì)數(shù)
1.引用簡(jiǎn)介
class Person{
var name:String
init(name:String) {
self.name = name
}
deinit {
print("\(name) is being deinitalized")
}
}
定義三個(gè)可選類型的Person,初始化自動(dòng)為nil
var person1:Person?
var person2:Person?
var person3:Person?
person1 = Person(name:"Harry") // 引用數(shù)量為1
person2 = person1 // 引用數(shù)量為2
person3 = person1 // 引用數(shù)量為3
此時(shí)Person的實(shí)例person已經(jīng)有了三個(gè)強(qiáng)引用了
person1 = nil // 引用數(shù)量為2
person2 = nil // 引用數(shù)量為1
person3 = nil // 引用數(shù)量為0 此時(shí)最后一個(gè)搶引用斷開熊锭,所以就會(huì)調(diào)用析構(gòu)函數(shù)deinit弧轧,打印 Harry is being deinitalized
2.類實(shí)例之間的循環(huán)強(qiáng)引用
** 在上面的例子中,ARC會(huì)跟蹤你所創(chuàng)建的Person實(shí)例的引用數(shù)量碗殷,并且會(huì)在Person實(shí)例不再被需要時(shí)銷毀它**
class Person2{
let name:String
init(name:String) {
self.name = name
}
var apartment:Apartment2?
deinit {
print("\(name) is being deinitialized")
}
}
class Apartment2 {
let unit:String
init(unit:String) {
self.unit = unit
}
var tenant:Person2?
deinit {
print("Apartment \(unit) is being deinitialized")
}
}
var john:Person2?
var unit4A:Apartment2?
john = Person2(name:"John Appleseed")
unit4A = Apartment2(unit:"4A")
下面這里就造成了循環(huán)引用
john!.apartment = unit4A
unit4A!.tenant = john
john = nil // 這里由于強(qiáng)引用互相持有精绎,不能釋放
3.弱引用
不幸的是,這兩個(gè)實(shí)例關(guān)聯(lián)后會(huì)產(chǎn)生一個(gè)循環(huán)強(qiáng)引用锌妻,對(duì)象各自之間相互持有代乃,我們可以用弱引用或無主引用
swift提供了兩種辦法用來解決你在使用類的屬性時(shí)所遇到的循環(huán)強(qiáng)引用問題:弱引用(weak reference)和無主引用(unowned reference)。弱引用不會(huì)對(duì)其引用的實(shí)例保持強(qiáng)引用仿粹,因?yàn)椴粫?huì)阻止ARC銷毀被引用的實(shí)例搁吓。這個(gè)特性阻止了引用變?yōu)檠h(huán)強(qiáng)引用。聲明屬性或者變量時(shí)吭历,在前面加上weak關(guān)鍵字表明這是一個(gè)弱引用堕仔。
class ApartmentL{
let unit:String
init(unit:String) {
self.unit = unit
}
weak var tenant:PersonL? // 注意這里是一個(gè)weak 表示弱引用
deinit {
print("Apartment \(unit) is being deinitialized")
}
}
class PersonL {
let name:String
init(name:String) {
self.name = name
}
var apartment:ApartmentL?
deinit {
print("\(name) is being deinitialized")
}
}
var johnl:PersonL?
var unit4Al:ApartmentL?
johnl = PersonL(name:"John Appleseed --- l")
unit4Al = ApartmentL(unit:"4A")
johnl!.apartment = unit4Al
unit4Al!.tenant = johnl
johnl = nil // 這里的johnl就可以釋放掉了
unit4Al = nil
注:PersonL實(shí)例依然保持對(duì)ApartmentL實(shí)例的強(qiáng)引用,但是ApartmentL實(shí)例只是對(duì)PersonL實(shí)例的弱引用晌区。這意味著當(dāng)你斷開johnl變量所保持的強(qiáng)引用時(shí)摩骨,再也沒有指向PersonL實(shí)例的強(qiáng)應(yīng)用了
4.無主引用
和弱引用類似,無主引用不會(huì)牢牢保持住引用的實(shí)例朗若。和弱引用不同的是恼五,無主引用是永遠(yuǎn)有值的。因此捡偏,無主引用總是被定義為非可選類型(nonoptional type)唤冈。你可以在聲明屬性或者變量時(shí),在前面加上關(guān)鍵字unowned表示這是一個(gè)無主引用银伟。
由于無主引用是非可選類型你虹,你不需要在使用它的時(shí)候?qū)⑺归_。無主引用總是可以被直接訪問彤避。不過ARC無法在市里被銷毀后將無主引用設(shè)為nil傅物,因此非可選類型的變量不允許被賦值為nil
class Customer{
let name:String
var card:CreditCard?
init(name:String) {
self.name = name
}
deinit {
print("\(name) is being deinitialized")
}
}
class CreditCard{
let number:UInt64
unowned let customer:Customer
init(number:UInt64,customer:Customer) {
self.number = number
self.customer = customer
}
deinit {
print("Card #\(number) is being deinitalized")
}
}
var johns:Customer?
johns = Customer(name:"John Appleseed --- s")
johns!.card = CreditCard(number:1234_5678_9012_3456,customer:johns!)
johns = nil