English 中文(简体)
• 迅速将某些财产价值从一个类别到另一个类别。
原标题:Copy some property values from one class to another class instance in Swift5

我写了一封信,迅速连接起来,并储存一个蓝色装置。 这一蓝色装置与传感器(例如,紫外线传感器、温度传感器等)实际连接,并将传感器的读物传递给传感器。

我有一个称为传感器的班子,每个类型的传感器都有几个子级,可以装入“蓝色”装置。 由于用户能够在这个蓝色装置的不同传感器中脱下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
}

我认为,我不是这一想法的狂热,因为它的规模非常高,如果我或一些未来的开发商为这一传感器类别增加新的财产,他们还必须记住在这种方法中添加这种财产。 我也尝试了思考,因为这种思考似乎像最有希望的方法一样,但却没有取得任何接近实现我所希望的东西的成功。

问题回答

你已经在这里进行了两个财产组合,并发表了评论和使用白色空间:

`` public class Sensor { // Properties I don’t want copied over to new class instance public let peripheral: CBPeripheral
// ... group 1

// Properties I do want copied over to new class instance
var bluetoothDeviceSerialNumber: String 
// ... group 2

Groupings like this are structs screaming to get out. You might try something like this:

```swift
public class Sensor {
    struct SensorDetails {
        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
    }

    // 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 sensorDeatils: SensorDetails

    required public init(withPeripheral peripheral: CBPeripheral) {
        self.peripheral = peripheral
    }
}

Now your copyPropertyValues function becomes trivial:

func copyPropertyValues(from oldSensor: Sensor) {
    self.sensorDetails = oldSensor.sensorDetails
}




相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...

热门标签