Objective c 一些for循环的问题--“for”;无法为类型为';双倍';索引类型为';int'&引用;错误

Objective c 一些for循环的问题--“for”;无法为类型为';双倍';索引类型为';int'&引用;错误,objective-c,swift,Objective C,Swift,我在索引的地方遇到了很多“不能为'Double'类型的值下标'int'类型的索引”错误?阵列?像这样 这段代码取自Obj-C,尽可能地翻译成Swift,但我不熟悉一些语法。我清理了for循环,但仍然存在一些Obj-C语法。你能帮我清理/重构我的代码吗 import UIKit import Foundation import AVFoundation let minFramesForFilterToSettle = 10 enum CurrentState { case statePau

我在索引的地方遇到了很多“不能为'Double'类型的值下标'int'类型的索引”错误?阵列?像这样

这段代码取自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
}
导入UIKit
进口基金会
进口AVF基金会
让minFramesForFilterToSettle=10
枚举当前状态{
案例状态暂停
病例状态抽样
}
类ViewController:UIViewController、AVCaptureVideoDataOutputSampleBufferDelegate{
let session=AVCaptureSession()
var摄像机:AVCaptureDevice?
var validFrameCounter:Int=0
var脉冲检测器:脉冲检测器
变量过滤器:过滤器//是否正确初始化?
var currentState=currentState.stateSampling//这是否正确初始化?
重写func viewDidLoad(){
super.viewDidLoad()
self.pulseDetector=pulseDetector()
self.filter=filter()//我需要这个吗?
startCameraCapture()//调用未使用的函数。要创建函数
}
重写函数didReceiveMemoryWarning(){
超级。我收到了记忆警告()
//处置所有可以重新创建的资源。
}
}
设NZEROS=10
设NPOLES=10
类过滤器{
变量xv=[Float](计数:NZEROS+1,重复值:0)
变量yv=[Float](计数:NPOLES+1,repeatedValue:0)
func processValue(值:Float)->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[9]=xv[10]=价值/收益;
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[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]);
返回yv[10];
}
}
设maxPeriod=1.5
设minPeriod=0.1
设invalidEntry=-11
设maxPeriodsToStore=20
设平均大小=20
类脉冲探测器{
变量UPVAL:[浮动]?
var下降值:[浮动]?
var upValIndex:[Int]?
var downValIndex:[Int]?
var lastVal:浮动?
开始:浮动?
var周期:双倍?
时间:两倍?
var周期指数:Int?
瓦尔:布尔?
变量频率:浮动?
var平均值:浮动?
var wasDown:布尔?
函数重置(){
对于变量i=0;iFloat{//在.h中声明的函数addNewValue
//我们跟踪零上和零下的值的数量
如果newVal>0{
UPVAL[upValIndex]=新值
upValIndex++
如果upValIndex>=平均大小{
upValIndex=0
}
}
如果newVal<0{
downVals[downValIndex]=-newVal
downValIndex++
如果downValIndex>=平均大小{
downValIndex=0
}
}
//计算出零以上的平均值
变量计数:浮动
var总计:浮动
对于变量i=0;i=(0.5*平均上升)和(下降){
wasDown=false
//计算出现在和上次发生的区别
如果time periodStartminPeriod{
周期[periodIndex]=时间周期开始
周期时间[周期索引]=时间
周期指数++
如果periodIndex>=maxPeriodsToStore{
周期索引=0
}
}
//跟踪转换发生的时间
周期开始=时间
}
//上下返回
如果newVal<(-0.5*平均向下){
返回-1
}如果newVal>(0.5*平均值以上),则为else{
返回1
}
返回0
}
让invalidPulsePeriod:Float=-1
func getAverage->Float{
变量时间:Double=CACurrentMediaTime()
变量总计:浮动=0
变量计数:浮点=0
对于变量i=0;i2{
返回总数/计数
}
返回无效脉冲周期
}
//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
#导入//导入AVFoundation
#定义从\u到\u存储的最大\u周期20//done
#定义平均大小20//done
#定义无效的\u脉冲\u周期-1//完成
@接口脉冲检测器:NSObject{
浮点数[平均大小];
浮动下降值[平均大小];
int-upValIndex;
int downValIndex;
浮动拉斯瓦尔;
浮动周期启动;
双句点[MAX_periods_TO_STORE];//这是一个数组!
双周期时间[最大周期到存储];//这是一个错误!!
整数周期指数;
布尔开始;
浮动频率;
浮动平均;
布尔倒下了;
}
@属性(非原子,赋值)float periodStart;//var periodStart=float?
-(float)addNewValue:(float)newVal atTime:(double)time;//声明一个名为addNewValue的方法,该方法有两个名为atTime和time的参数,返回一个float
-(float)getAverage;//声明一个名为getAverage的方法,该方法返回一个float
-(void)reset;//声明一个不返回任何内容的方法
@结束

对于每个

var periods : [Double] = Array (count: AVERAGE_SIZE, repeatedValue: 0.0)
var upVals: [Float]
var downVals: [Float]
var periods: [Double]
var periodTimes: [Double]