一些for循环的问题 - "不能下标类型的值' Double'索引类型' int'"错误

时间:2015-04-21 03:13:23

标签: objective-c swift

我得到了很多“不能在带有索引的地方使用类型为'int'”的索引下标类型'Double'的值吗?数组?像这样

enter image description here

这段代码取自Obj-C并尽可能地翻译成Swift,但我对某些语法不熟悉。我清理了for循环但是仍然存在一些Obj-C语法。你能帮我清理/重构我的代码吗?

import UIKit
import Foundation
import AVFoundation


let minFramesForFilterToSettle = 10

enum CurrentState {
case statePaused
case stateSampling
}

class ViewController: UIViewController, AVCaptureVideoDataOutputSampleBufferDelegate {

let session = AVCaptureSession()
var camera : AVCaptureDevice?
var validFrameCounter: Int = 0
var pulseDetector: PulseDetector
var filter: Filter                           // Is this initialized correctly?
var currentState = CurrentState.stateSampling       // Is this initialized correctly?

override func viewDidLoad() {
    super.viewDidLoad()
    self.pulseDetector = PulseDetector()
    self.filter = Filter()    // Do I need this?
    startCameraCapture() // call to un-used function. TO DO create function
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}



}

let NZEROS = 10
let NPOLES = 10

class Filter {

var xv = [Float](count: NZEROS + 1, repeatedValue: 0)
var yv = [Float](count: NPOLES + 1, repeatedValue: 0)

func processValue(value: Float) -> Float {

    let gain: Float = 1.894427025e+01

    xv[0] = xv[1]; xv[1] = xv[2]; xv[2] = xv[3]; xv[3] = xv[4]; xv[4] = xv[5]; xv[5] = xv[6]; xv[6] = xv[7]; xv[7] = xv[8]; xv[8] = xv[9]; xv[9] = xv[10]; xv[10] = value / gain;
    yv[0] = yv[1]; yv[1] = yv[2]; yv[2] = yv[3]; yv[3] = yv[4]; yv[4] = yv[5]; yv[5] = yv[6]; yv[6] = yv[7]; yv[7] = yv[8]; yv[8] = yv[9]; yv[9] = yv[10];
    yv[10] =   (xv[10] - xv[0]) + 5 * (xv[2] - xv[8]) + 10 * (xv[6] - xv[4])
    + ( -0.0000000000 * yv[0]) + (  0.0357796363 * yv[1])
    + ( -0.1476158522 * yv[2]) + (  0.3992561394 * yv[3])
    + ( -1.1743136181 * yv[4]) + (  2.4692165842 * yv[5])
    + ( -3.3820859632 * yv[6]) + (  3.9628972812 * yv[7])
    + ( -4.3832594900 * yv[8]) + (  3.2101976096 * yv[9]);
    return yv[10];
}

}

let maxPeriod = 1.5
let minPeriod = 0.1
let invalidEntry = -11
let maxPeriodsToStore = 20
let averageSize = 20


class PulseDetector {

var upVals: [Float]?
var downVals: [Float]?
var upValIndex: [Int]?
var downValIndex: [Int]?
var lastVal: Float?
var periodStart: Float?
var periods: Double?
var periodTimes: Double?
var periodIndex: Int?
var started: Bool?
var freq: Float?
var average: Float?
var wasDown: Bool?


func reset() {


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

func addNewValue(newVal:Float, atTime:Double) -> Float {     // the function addNewValue that was declared in .h
// 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 {
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) {
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
}


let invalidPulsePeriod:Float = -1

func getAverage -> Float {
    var time: Double = CACurrentMediaTime()
    var total:Float = 0
    var count:Float = 0
for var i = 0; i < maxPeriodsToStore; i++ {
// only use upto 10 seconds worth of data
if periods[i] != invalidEntry && time-periodTimes[i] < 10 {
count++
total+=periods[i]
}
}
// do we have enough values?
if count > 2 {
return total/count
}
return invalidPulsePeriod
}

// Obj-C文件

#import <Foundation/Foundation.h>  // import AVFoundation

#define MAX_PERIODS_TO_STORE 20 // done
#define AVERAGE_SIZE 20 // done
#define INVALID_PULSE_PERIOD -1 // done

@interface PulseDetector : NSObject {  
float upVals[AVERAGE_SIZE];
float downVals[AVERAGE_SIZE];
int upValIndex;
int downValIndex;

float lastVal;
float periodStart;
double periods[MAX_PERIODS_TO_STORE];  //  this is an array!
double periodTimes[MAX_PERIODS_TO_STORE]; // this is an rray !!

int periodIndex;
bool started;
float freq;
float average;

bool wasDown;
}

@property (nonatomic, assign) float periodStart;  // var periodStart = float?


-(float) addNewValue:(float) newVal atTime:(double) time; // declaring a method called addNewValue with 2 arguments called atTime and time that returns a float
-(float) getAverage; // declaring a method called getAverage that returns a float
-(void) reset; // declaring a method that returns nothing

@end

2 个答案:

答案 0 :(得分:2)

[i]periodsupValsperiodTimes等每个人使用downVals的数组下标语法。但是,所有这些变量都是定义为Double?,它是一个可选的双精度,而绝对不是一个数组。因此错误。

获得与Objective-C等效的东西将从声明开始,如:

var periods : [Double] = Array (count: AVERAGE_SIZE, repeatedValue: 0.0)

答案 1 :(得分:0)

我认为您需要将要尝试下标的属性声明为数组:

var upVals: [Float]
var downVals: [Float]
var periods: [Double]
var periodTimes: [Double]