我写了一封信,迅速连接起来,并储存一个蓝色装置。 这一蓝色装置与传感器(例如,紫外线传感器、温度传感器等)实际连接,并将传感器的读物传递给传感器。
我有一个称为传感器的班子,每个类型的传感器都有几个子级,可以装入“蓝色”装置。 由于用户能够在这个蓝色装置的不同传感器中脱下ug和ug,我根据传感器类型建立了我的子级的新例子,我用这个新话取代旧的传感器级。 然而,我没有发现一种很好的方法复制一些仍适用于新传感器的相关财产(我不希望所有被复制的财产,因为其中一些财产是专门根据类别确定的,而其他财产将承接,而不论传感器是否被装入蓝色装置)。
public class Sensor {
// Properties I don’t want copied over to new class instance
public let peripheral: CBPeripheral
var sensorID: Int
var sensorImage: UIImage
var modelName: String
var sensorType: String
// Properties I do want copied over to new class instance
var bluetoothDeviceSerialNumber: String
var hardwareVersion: String
var firmwareVersion: String
var softwareVersion: String
var alias: String
var lastConnectedDate: Date
var exampleProperty1: Int
var exampleProperty2: Int
var exampleProperty3: Int
var exampleProperty4: Bool
var exampleProperty5: Bool
required public init(withPeripheral peripheral: CBPeripheral) {
self.peripheral = peripheral
}
}
class UV_Sensor: Sensor {
required public init(withPeripheral peripheral: CBPeripheral){
super.init(withPeripheral: peripheral)
var sensorID = 1
var sensorImage = UIImage(named: “UV_Sensor”)
var modelName = “UVSensorModelName”
var sensorType = “UV”
}
}
Class TemperatureSensor: Sensor {
required public init(withPeripheral peripheral: CBPeripheral){
super.init(withPeripheral: peripheral)
var sensorID = 2
var sensorImage = UIImage(named: “Temp_Sensor”)
var modelName = “TemperatureSensorModelName”
var sensorType = “Temp”
}
}
我目前的解决办法是,采用一种方法,人工复制我所希望的变数。
func copyPropertyValues(from oldSensor: Sensor) {
self.bluetoothDeviceSerialNumber = oldSensor.bluetoothDeviceSerialNumber
self.hardwareVersion = oldSensor.hardwareVersion
self.firmwareVersion = oldSensor.firmwareVersion
self.softwareVersion = oldSensor.softwareVersion
self.alias = oldSensor.alias
self.lastConnectedDate = oldSensor.lastConnectedDate
self.exampleProperty1 = oldSensor.exampleProperty1
self.exampleProperty2 = oldSensor.exampleProperty2
self.exampleProperty3 = oldSensor.exampleProperty3
self.exampleProperty4 = oldSensor.exampleProperty4
self.exampleProperty5 = oldSensor.exampleProperty5
}
我认为,我不是这一想法的狂热,因为它的规模非常高,如果我或一些未来的开发商为这一传感器类别增加新的财产,他们还必须记住在这种方法中添加这种财产。 我也尝试了思考,因为这种思考似乎像最有希望的方法一样,但却没有取得任何接近实现我所希望的东西的成功。