这里有下标错误,但我不确定原因。这次我确保匹配的类型,但现在我收到错误,因为他们匹配。
不能使用“Double”类型的索引下标“[Double]”类型的值
let maxPeriod:Double = 1.5
let minPeriod:Double = 0.1
let invalidEntry:Float = -100
let maxPeriodsToStore:Double = 20
let averageSize:Int = 20
class Detector {
let session = AVCaptureSession()
var camera : AVCaptureDevice?
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:Double = 0; i < maxPeriodsToStore; i++ {
periods[i] = invalidEntry // error: Cannot subscript a value of type '[Double]' with an index of type 'Double'
}
for var i:Int = 0; i < averageSize; i++ {
upVals[i] = invalidEntry
downVals[i] = invalidEntry
}
freq = 0.5
periodIndex = 0
downValIndex = 0
upValIndex = 0
}
原始Objective-C代码
// Detector.m
#import <QuartzCore/QuartzCore.h>
#import "Detector.h"
#import <vector>
#import <algorithm>
#define MAX_PERIOD 1.5
#define MIN_PERIOD 0.1
#define INVALID_ENTRY -100
@implementation Detector
@synthesize periodStart;
- (id) init
{
self = [super init];
if (self != nil) {
// set everything to invalid
[self reset];
}
return self;
}
-(void) reset {
for(int i=0; i<MAX_PERIODS_TO_STORE; i++) {
periods[i]=INVALID_ENTRY;
}
for(int i=0; i<AVERAGE_SIZE; i++) {
upVals[i]=INVALID_ENTRY;
downVals[i]=INVALID_ENTRY;
}
freq=0.5;
periodIndex=0;
downValIndex=0;
upValIndex=0;
}
// Detector.h
#import <Foundation/Foundation.h>
#define MAX_PERIODS_TO_STORE 20
#define AVERAGE_SIZE 20
#define INVALID_PERIOD -1
@interface Detector : NSObject {
float upVals[AVERAGE_SIZE];
float downVals[AVERAGE_SIZE];
int upValIndex;
int downValIndex;
float lastVal;
float periodStart;
double periods[MAX_PERIODS_TO_STORE];
double periodTimes[MAX_PERIODS_TO_STORE];
int periodIndex;
bool started;
float freq;
float average;
bool wasDown;
}
@property (nonatomic, assign) float periodStart;
-(float) addNewValue:(float) newVal atTime:(double) time;
-(float) getAverage;
-(void) reset;
@end
答案 0 :(得分:0)
如果您要将大量代码粘贴到您的问题中,您应该清楚地指出哪条线路出错。
我确实看了你的代码并发现了问题。您不应该使用带有Double的for循环来索引数组。而是改变你的for循环以使用Int变量,然后如果你需要用其他双精度数学进行数学运算,则将其强制转换为Double。