如何让deinit在swift生效
how to make deinit take effect in swift
我有车class。假设一辆汽车开往垃圾场,这辆车不应再计入总人口。我有 deinit 函数,但如何系统地从汽车数量中删除汽车?也就是说,如何让deinit生效呢?
我有一个 class 变量 isJunk
但不知道如何使用它来完成这项工作。
class Car {
static var population: Int = 0
var isJunk: Bool = false
var color: String
var capacity: Int
var driver: Bool?
var carOn: Bool = false
init (carColor: String, carCapacity: Int) {
self.capacity = carCapacity
self.color = carColor
Car.population += 1
}
deinit {
Car.population -= 1
}
func startCar() {
self.carOn = true
}
}
class Car {
static var population: Int = 0
init() {
Car.population += 1
}
deinit {
Car.population -= 1
}
}
var cars: [Car] = [Car(), Car()]
print("Population:", Car.population) // "Population: 2"
// now the second car is removed from array and we have no other references to it
// it gets removed from memory and deinit is called
cars.removeLast()
print("Population:", Car.population) // "Population: 1"
然而,同样可以通过询问 cars
数组中的项目数来实现。这通常是比私有实例计数器更好的选择。
为了将项目保存在内存中,您总是需要为它们设置某种寄存器(例如数组)。那个寄存器可以让他们计数。
一种可能:
class CarPopulation {
var liveCars: [Car] = []
var junkCars: [Car] = []
}
或者你可以将它们放在一个数组中并在汽车上设置 junk
并在需要时计算非垃圾车:
class CarPopulation {
var cars: [Car] = []
func liveCars() -> Int {
return self.cars.filter { ![=12=].junk }.count
}
}
有很多可能性,但将计数器提取给其他拥有汽车的 class 可能是更好的解决方案。
当您释放 Car
的实例时(当您完全摆脱该对象的实例时),将调用 deinit
。当您将 Car
实例放入垃圾场时,我认为您并不想摆脱 Car
实例,您实际上只是想更改它的位置。我会建议使用不同的函数来处理更改 Car
.
的位置
也许:
func changeLocation(newLocation: String) {
// Perhaps add an instance variable to 'remember' the location of the car
switch newLocation {
case "junkyard":
Car.population -= 1
default:
// Perhaps check whether previous location was Junkyard and increment
// counter if the Car is coming out of the Junkyard
print("Unrecognized location")
}
}
我有车class。假设一辆汽车开往垃圾场,这辆车不应再计入总人口。我有 deinit 函数,但如何系统地从汽车数量中删除汽车?也就是说,如何让deinit生效呢?
我有一个 class 变量 isJunk
但不知道如何使用它来完成这项工作。
class Car {
static var population: Int = 0
var isJunk: Bool = false
var color: String
var capacity: Int
var driver: Bool?
var carOn: Bool = false
init (carColor: String, carCapacity: Int) {
self.capacity = carCapacity
self.color = carColor
Car.population += 1
}
deinit {
Car.population -= 1
}
func startCar() {
self.carOn = true
}
}
class Car {
static var population: Int = 0
init() {
Car.population += 1
}
deinit {
Car.population -= 1
}
}
var cars: [Car] = [Car(), Car()]
print("Population:", Car.population) // "Population: 2"
// now the second car is removed from array and we have no other references to it
// it gets removed from memory and deinit is called
cars.removeLast()
print("Population:", Car.population) // "Population: 1"
然而,同样可以通过询问 cars
数组中的项目数来实现。这通常是比私有实例计数器更好的选择。
为了将项目保存在内存中,您总是需要为它们设置某种寄存器(例如数组)。那个寄存器可以让他们计数。
一种可能:
class CarPopulation {
var liveCars: [Car] = []
var junkCars: [Car] = []
}
或者你可以将它们放在一个数组中并在汽车上设置 junk
并在需要时计算非垃圾车:
class CarPopulation {
var cars: [Car] = []
func liveCars() -> Int {
return self.cars.filter { ![=12=].junk }.count
}
}
有很多可能性,但将计数器提取给其他拥有汽车的 class 可能是更好的解决方案。
当您释放 Car
的实例时(当您完全摆脱该对象的实例时),将调用 deinit
。当您将 Car
实例放入垃圾场时,我认为您并不想摆脱 Car
实例,您实际上只是想更改它的位置。我会建议使用不同的函数来处理更改 Car
.
也许:
func changeLocation(newLocation: String) {
// Perhaps add an instance variable to 'remember' the location of the car
switch newLocation {
case "junkyard":
Car.population -= 1
default:
// Perhaps check whether previous location was Junkyard and increment
// counter if the Car is coming out of the Junkyard
print("Unrecognized location")
}
}