我改变了

-(float) addNewValue:(float) newVal atTime:(double) time {


func addNewValue(newVal: CFloat, atTime time: CDouble) -> CFloat {}

没有错误,但我不知道为什么会这样。为什么FloatDouble会变成CFloatCDouble
也没有以下错误:
func addNewValue(newVal: Float, atTime time: Double) -> Float {}

那我该用哪一个呢?
完整代码
let maxPeriod:Double = 1.5
let minPeriod:Double = 0.1
let invalidEntry = -100
let maxPeriodsToStore = 20
let averageSize:Int = 20


class Detector {

    let session = AVCaptureSession()
    var camera : AVCaptureDevice?

    var upVals = [averageSize]
    var downVals = [averageSize]
    var upValIndex: Int?
    var downValIndex: Int?

    var lastVal: Float?
    var periodStart: Float?
    var periods = [maxPeriodsToStore]
    var periodTimes = [maxPeriodsToStore]

    var periodIndex: Int?
    var started: Bool?
    var freq: Float?
    var average: Float?

    var wasDown: Bool?


    func reset() {


        for var i:Int = 0; i < maxPeriodsToStore; i++ {
            periods[i] = invalidEntry
        }
        for var i:Int = 0; i < averageSize; i++ {
            upVals[i] = invalidEntry
            downVals[i] = invalidEntry
        }
        freq = 0.5
        periodIndex = 0
        downValIndex = 0
        upValIndex = 0
    }

    // SO Question is regarding this function

    func addNewValue(newVal: Float, atTime time: Double) -> Float {
    // we keep track of the number of values above and below zero
    if newVal > 0 {
    upVals[upValIndex!] = newVal
    upValIndex!++
    if upValIndex >= averageSize {
    upValIndex = 0
    }
    }
    if newVal < 0 {
    downVals[downValIndex!] -= newVal
    downValIndex!++
    if downValIndex! >= averageSize as! Int {
    downValIndex = 0
    }
    }
    // work out the average value above zero
    var count: Float
    var total: Float
    for var i=0; i < averageSize; i++ {
    if upVals[i] != invalidEntry {
    count++
    total += upVals[i]
    }
    }
    var averageUp = total/count
    // and the average value below zero
    count=0;
    total=0;
    for var i=0; i < averageSize; i++ {
    if downVals[i] != invalidEntry {
    count++
    total+=downVals[i]
    }
    }
    var averageDown = total/count

    // is the new value a down value?
    if newVal < (-0.5*averageDown) {
    wasDown = true
    }

    // is the new value an up value and were we previously in the down state?
    if (newVal >= (0.5*averageUp) && (wasDown) != nil) {
    wasDown = false

    // work out the difference between now and the last time this happenned
    if (time - periodStart) < maxPeriod && (time - periodStart) > minPeriod {
    periods[periodIndex]=time-periodStart
    periodTimes[periodIndex]=time
    periodIndex++
    if periodIndex >= maxPeriodsToStore {
                periodIndex = 0
    }
    }
    // track when the transition happened
    periodStart = time
    }
    // return up or down
    if newVal < (-0.5*averageDown) {
    return -1
    } else if newVal > (0.5*averageUp) {
    return 1
    }
    return 0
    }

最佳答案

那我该用哪一个呢?
浮动和双倍。术语CFloat和CDouble只是它们的类型别名(同义词),因此没有必要使用它们;它们只是为了兼容性。

09-18 02:47