Swift继承问题泛型

时间:2015-05-23 16:06:34

标签: swift generics inheritance

我需要能够调用泛型类型的方法,以便我可以从SensorDescBattery获得常量SENSOR_ID的值。 如果调用getSensorId()我使用的方法 但是当我使用泛型类型sensorDescBattery调用该函数时,它会向我询问方法getSensorId()的参数,但它没有。[/ p>

我在下面介绍了SensorDescBattery类:

class SensorDescBattery: SensorDescSingleValue {


    let SENSOR_ID : UInt64 = 0x0000000000000001 { get{ return SENSOR_ID}}

    var sensorIdentifier: Int64 = 0x0000000000000001

    var timestamp: UInt64
    var batteryPercent: Float
    var isCharging: Bool
    var isUsbCharge: Bool
    var isAcCharge: Bool

    init(timestamp: UInt64, batteryPercent: Float, isCharging: Bool, isUsbCharge: Bool, isAcCharge: Bool) {

        self.timestamp = timestamp
        self.batteryPercent = batteryPercent;
        self.isCharging = isCharging;
        self.isUsbCharge = isUsbCharge;
        self.isAcCharge = isAcCharge;
    }

    /*
        override keyword always needed here
    */
    required init(sensorData: SensorUploadSensorData) {

        self.timestamp = sensorData.recordTime
        self.batteryPercent = sensorData.valueFloat[0]
        self.isCharging = sensorData.valueBool[0]
        self.isUsbCharge = sensorData.valueBool[1]
        self.isAcCharge = sensorData.valueBool[2]
    }

    init (battery: UIDevice, timestamp: UInt64) {
        self.timestamp = timestamp
        self.batteryPercent = battery.batteryLevel
        if UIDeviceBatteryState.Charging.rawValue == 3{
            self.isCharging = true
            self.isUsbCharge = false
            self.isAcCharge = true
        }
        else {
            self.isCharging = false
            self.isUsbCharge = false
            self.isAcCharge = false
        }
    }

    /*
        Override keyword needed
    */
    func toProtoSensor() -> SensorUploadSensorData {

        let builder = SensorUploadSensorData.builder()

        /*
            Always set the record time! also it's important to put the values in the
            array in the same exact order as in Android
        */
        builder.recordTime = timestamp
        builder.valueFloat = [self.batteryPercent]
        builder.valueBool = [self.isCharging, self.isUsbCharge, self.isAcCharge]

        return builder.build()
    }

    func getSensorId() -> UInt64 {
        return SENSOR_ID;
    }

    func getValue() -> Float {
        return self.batteryPercent;
    }
}

这是QueryNumSingleValue类:

class QueryNumSingleValue {

var List : Array<SensorUploadSensorData>

func getSensorID() -> UInt64{
    fatalError("Must Override")
}
init(from timestamp_from :UInt64,to timestamp_to : UInt64){
    let vm = NervousVM()

    self.List = vm.retrieve(0, fromTimeStamp: 0, toTimeStamp: 0)
    self.List = vm.retrieve(getSensorID(), fromTimeStamp: timestamp_from, toTimeStamp: timestamp_to)
    if(containsReading()){
        println("retreived list of size /(getCount())")
    }
}


func getCount() -> Int
{
return List.count
}

这是SensorDescSingleValue

protocol SensorDescSingleValue : SensorDesc {

func getValue() -> Float

}

我要做的是以下内容:

class SensorQueriesBattery<T : SensorDescBattery> : QueryNumSingleValue<SensorDescBattery>{

    override func getSensorID() -> UInt64 {
        return T.getSensorId()
    }

}

但它不让我。 我该怎么办?

1 个答案:

答案 0 :(得分:2)

编译器要求你输入一个参数,因为你在这部分代码中使用的是Class而不是对象来调用方法(并且方法不是静态的):

override func getSensorID() -> UInt64 {
    return T.getSensorId()
}

您应该阅读有关generics in swift

的更多信息

使代码适用于您的方法的一种方法是:

class SensorQueriesBattery<T : SensorDescBattery> : QueryNumSingleValue{

    var batteryDesc: T

    init(desc:T) {
        self.batteryDesc = desc
    }

    override func getSensorID() -> UInt64 {
            return self.batteryDesc.getSensorId()
    } 
}

您必须在init中传递Generic对象才能调用方法getSensorId()