1

I Need to be able to call a method of the generic type, so that I can have the value of a constant SENSOR_ID from SensorDescBattery. The method I use if called getSensorId() But when I call that function using generic type sensorDescBattery, it asks me for a parameter to the method getSensorId(),but it does not have one.

I have presented the class SensorDescBattery below:

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;
    }
}

Here is the class 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
}

This is SensorDescSingleValue

protocol SensorDescSingleValue : SensorDesc {

func getValue() -> Float

}

What I am trying to do is the following :

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

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

}

But it does not let me. How can I proceed?

1 Answer 1

2

The compiler is asking you for a parameter because you are using the Class and not the object to call the method (and the method is no static) in this part of the code:

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

You should read more about generics in swift

One way to make your code works with your approach is:

class SensorQueriesBattery<T : SensorDescBattery> : QueryNumSingleValue{

    var batteryDesc: T

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

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

You have to pass the Generic object in the init to be able to call the method getSensorId()

Sign up to request clarification or add additional context in comments.

4 Comments

OP can just do some more reading about generics in general, this is an issue that haunts all implementations of them that I have seen (C++, Java, Swift).
I tried your method,and all the lines give the error QueryNumSingleValue needs a generic type within <...>
@LoveMeow Have you changed the definition of QueryNumSingleValue class as a generic? if so, you have to set the generic when inherits from ´QueryNumSingleValue´.
I tweaked it a bit, like you said the problem was not creating an object!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.