title: how to represent a zero or nil double value in NSMutableData?
body: <p>I have an array of double values that should be stored in an external file. I convert the array to NSMutableData and save the data to disk.</p>
  
  <p>Some of the double values are either missing (nil) or are zero (0.0).
  Missing values can be replaced by 0.0 or by the maximum double representation.</p>
  
  <p>But Double has no max value (like Int.max) and 0.0 values are interpreted by NSData as termination bytes. So every time a 0.0 value is encountered, the data ends too soon.</p>
  
  <pre><code>var description: String {
      var output: String = ""
      for value in self
      {
          output = output + "\(value)\n"
      }
      return output
  }
  
  var count : Int { return data.length/sizeof(Double) }
  
  func append(var value: Double)
  {
      self.data.appendBytes(&amp;value, length: self.size)
  }
  
  subscript(index: Int) -&gt; Double?
  {
      assert(index &lt; self.count, "index exceeds bounds")
      var output : Double?
      self.data.getBytes(&amp;output, range: NSRange(location: index * self.size, length: self.size))
      return output
  }
  </code></pre>
  
  <p><strong>edit:</strong></p>
  
  <p>D'Oh, I was using generate to print out the values, and GeneratorOf stops after the first nil value. Still, what would be a good way to represent a nil value to differentiate from 0.0? </p>
  
  <pre><code>func generate() -&gt; GeneratorOf&lt;Double&gt;
  {
      var index = 0
      var count = self.counter
      return GeneratorOf&lt;Double&gt; {
          if index &lt; count
          {
              return self[index++]
          }
          return nil
      }
  }
  </code></pre>
  
  <p><strong>edit2</strong></p>
  
  <p>It's Double.infinity rather than Int.max</p>
  
  <p>I need to wake up now. </p>