title: Can’t seem to avoid force unwrapping in Swift
body: <p><strong>Update</strong>: this is regarding a Mac app created with the Document-based Application template in Xcode, and I’m overriding </p>
  
  <pre><code>override func readFromFileWrapper(fileWrapper: NSFileWrapper, ofType typeName: String, error outError: NSErrorPointer) -&gt; Bool
  </code></pre>
  
  <hr>
  
  <p>I’m trying to read in a file from an NSFileWrapper and it seems like I can't get away from having at least one <code>!</code> in there.</p>
  
  <p>First, I tried</p>
  
  <pre><code>if let rtfData = files["textFile.rtf"]?.regularFileContents,
          newString = NSMutableAttributedString(data: rtfData, options: [NSDocumentTypeDocumentAttribute : NSRTFTextDocumentType], documentAttributes: nil, error: nil) {
                  text = newString
                  return true
          }
  </code></pre>
  
  <p>So I get this error</p>
  
  <blockquote>
    <p>Value of optional type 'NSData?' not unwrapped; did you mean to use '!' or '?'?</p>
  </blockquote>
  
  <p>And I have to either cast <code>regularFileContents as NSData!</code> or I have to force unwrap it in the NSMutableAttributedString's initializer (<code>data: rtfData!</code>).</p>
  
  <hr>
  
  <p>So then I tried</p>
  
  <pre><code>let rtfData = files["textFile.rtf"]?.regularFileContents
  
  if (rtfData != nil) {
      if let newString = NSMutableAttributedString(data: rtfData, options: [NSDocumentTypeDocumentAttribute : NSRTFTextDocumentType], documentAttributes: nil, error: nil) {
          text = newString
          return true
      }
  }
  </code></pre>
  
  <p>Which results in </p>
  
  <blockquote>
    <p>Cannot find an initializer for type 'NSMutableAttributedString' that accepts an argument list of type '(data: NSData??, options: [String : String], documentAttributes: nil, error: nil)'"</p>
  </blockquote>
  
  <p>So I have to change the initializer to say <code>data: rtfData!!</code>, which I'm not even sure what that <em>means</em>.</p>
  
  <p>Or, I can cast <code>regularFileContents as NSData?</code>, and then I can only use one <code>!</code> in the initializer.</p>
  
  <p><strong>Update:</strong> Here's another thing I’ve tried since posting. I figured the double <code>?</code> in <code>NSData??</code> could be due to me optionally unwrapping the NSFileWrapper <code>(files["textFile.rtf"]?</code>) so I tried this:</p>
  
  <pre><code>if let rtfWrapper = files["textFile.rtf"],
      rtfData = rtfWrapper.regularFileContents,
      newString = NSMutableAttributedString(data: rtfData, options: [NSDocumentTypeDocumentAttribute : NSRTFTextDocumentType], documentAttributes: nil, error: nil) {
          text = newString
          return true
      }
  </code></pre>
  
  <p>The compiler <strong>still</strong> wants me to force unwrap NSData.</p>
  
  <hr>
  
  <p>I am pretty thoroughly confused.</p>
  
  <p>What is <code>NSData??</code>, why is it “doubly” optional, why is it the result of <code>.regularFileContents</code>, and how can I <em>safely</em>, without <code>!</code>-ing all the way, get the NSData and pass it to the NSMutableAttributedString’s intializer?</p>
  
  <hr>
  
  <p><strong>Another update</strong></p>
  
  <p>I figured maybe the <code>files</code> constant could be the source of another optional wrapping:</p>
  
  <pre><code>let files = fileWrapper.fileWrappers
  </code></pre>
  
  <p>But the compiler won't let me <code>if let</code> it. If I try, for example, </p>
  
  <pre><code>if let files = fileWrapper.fileWrappers {
      if let rtfFile = files["textFile.rtf"] {
          if let rtfData = rtfFile.regularFileContents {
              if let newString = NSMutableAttributedString(data: rtfData, options: [NSDocumentTypeDocumentAttribute : NSRTFTextDocumentType], documentAttributes: nil, error: nil) {
                  text = newString
                  return true
              }
          }
      }
  }
  </code></pre>
  
  <p>The compiler gives this error:</p>
  
  <blockquote>
    <p>Bound value in a conditional binding must be of Optional type</p>
  </blockquote>
  
  <p>About <code>fileWrapper.fileWrappers</code></p>
  
  <p>The <code>fileWrapper</code> variable is a parameter of the method, which is </p>
  
  <pre><code>override func readFromFileWrapper(fileWrapper: NSFileWrapper, ofType typeName: String, error outError: NSErrorPointer) -&gt; Bool
  </code></pre>