title: Cannot subscript a value of type 'Double' with an index of type 'Double'
body: <p>Have a subscript error here but I'm not sure why. This time I made sure the types matched but now I'm getting an error because they DO match.  </p>
  
  <blockquote>
    <p>Cannot subscript a value of type '[Double]' with an index of type 'Double' </p>
  </blockquote>
  
  <pre><code>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 &lt; 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 &lt; averageSize; i++ {
              upVals[i] = invalidEntry
              downVals[i] = invalidEntry
          }
          freq = 0.5
          periodIndex = 0
          downValIndex = 0
          upValIndex = 0
      }
  </code></pre>
  
  <p>Original Objective-C code</p>
  
  <pre><code>// Detector.m
  
  #import &lt;QuartzCore/QuartzCore.h&gt;
  #import "Detector.h"
  #import &lt;vector&gt;
  #import &lt;algorithm&gt;
  
  #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&lt;MAX_PERIODS_TO_STORE; i++) {
      periods[i]=INVALID_ENTRY;
  }
  for(int i=0; i&lt;AVERAGE_SIZE; i++) {
      upVals[i]=INVALID_ENTRY;
      downVals[i]=INVALID_ENTRY;
  }   
    freq=0.5;
    periodIndex=0;
    downValIndex=0;
    upValIndex=0;
  }
  
  
  // Detector.h
  
  
  #import &lt;Foundation/Foundation.h&gt;
  
  #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
  </code></pre>