title: Can I throw from class init() in Swift with constant string loaded from file?
body: <p>I'd like my class <em>init()</em> in Swift to throw an error if something goes wrong with loading a file into a string within the class. Once the file is loaded, the string will not be altered, so I would prefer to use <em>let</em>. This works:</p>
  
  <pre><code>class FileClass {    
      var text: NSString = ""   
      init() throws {   
          do {
              text = try NSString( contentsOfFile: "/Users/me/file.txt", encoding: NSUTF8StringEncoding ) }
          catch let error as NSError {
              text = ""
              throw error
          }      
      }
  }
  </code></pre>
  
  <p>but when I replace</p>
  
  <pre><code>var text: NSString = ""
  </code></pre>
  
  <p>with</p>
  
  <pre><code>let text: NSString
  </code></pre>
  
  <p>I get an <strong>All stored properties of a class instance must be initialized before throwing from an initializer</strong> error.</p>
  
  <p>I've tried various approaches such as to make <em>text</em> optional</p>
  
  <pre><code>let text: NSString?
  </code></pre>
  
  <p>but haven't found any that work. It it possible to have <em>text</em> be loaded from a file, immutable, and for <em>init()</em> to throw an error? Can I have my cake and eat it three?</p>
  
  <p>Many thanks in advance!</p>