swift - Cannot subscript a value of type 'Double' with an index of type 'Double' -
have subscript error here i'm not sure why. time made sure types matched i'm getting error because match.
cannot subscript value of type '[double]' index of type '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() { var i:double = 0; < maxperiodstostore; i++ { periods[i] = invalidentry // error: cannot subscript value of type '[double]' index of type 'double' } var i:int = 0; < averagesize; i++ { upvals[i] = invalidentry downvals[i] = invalidentry } freq = 0.5 periodindex = 0 downvalindex = 0 upvalindex = 0 }
original objective-c code
// 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 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
if you're going paste mess of code question, should give clear indication of line giving error.
i did @ code , found problem. should not use loop double index array. instead change loop use int variable, , cast double if need math with other doubles.
Comments
Post a Comment