gpt4 book ai didi

objective-c - 一些for循环的问题 - - "Cannot subscript a value of type ' Double' with an index of type 'int' "errors

转载 作者:可可西里 更新时间:2023-11-01 01:40:59 25 4
gpt4 key购买 nike

我在有索引的地方收到很多“无法使用‘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

最佳答案

您正在为每个 periodsupValsperiodTimes[i] 使用数组下标语法, downVals 等。但是,所有这些变量都被定义为 Double?,这是一个可选的 double 值,绝对不是数组。因此错误。

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

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

关于objective-c - 一些for循环的问题 - - "Cannot subscript a value of type ' Double' with an index of type 'int' "errors,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29762242/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com