title: iOS App Crash: Thread 9: Fatal error: UnsafeBufferPointer has a nil start and nonzero count
body: <p>App is crashing at following line</p>
  <p><code>cid = &quot;CID: \(String(describing: Singleton.shared.settings.userName ?? &quot;NA&quot;))&quot;</code></p>
  <p>Crashing Code:</p>
  <pre><code>static func clientIdFor(basicToken: String) -&gt; String {
          var cid: String = &quot;&quot;
          if let encodedData = Data(base64Encoded: basicToken, options: .ignoreUnknownCharacters) {
              let encodedString = String(data: encodedData, encoding: .utf8) ?? &quot;&quot;
              if encodedString.contains(&quot;:&quot;) {
                  cid = encodedString.components(separatedBy: &quot;:&quot;).first ?? &quot;NA&quot;
              } else {
                  cid = &quot;CID: \(String(describing: Singleton.shared.settings.userName ?? &quot;NA&quot;))&quot;  /// &lt;- CRASH
              }
          }
          return cid
      }
  </code></pre>
  <p>Here is backtrace</p>
  <pre><code> bt
  * thread #9, queue = 'com.apple.root.background-qos', stop reason = Fatal error: UnsafeBufferPointer has a nil start and nonzero count
      frame #0: 0x00007fff30a08770 libswiftCore.dylib`_swift_runtime_on_report
      frame #1: 0x00007fff30a87579 libswiftCore.dylib`_swift_stdlib_reportFatalErrorInFile + 201
      frame #2: 0x00007fff30740c13 libswiftCore.dylib`closure #1 (Swift.UnsafeBufferPointer&lt;Swift.UInt8&gt;) -&gt; () in closure #1 (Swift.UnsafeBufferPointer&lt;Swift.UInt8&gt;) -&gt; () in closure #1 (Swift.UnsafeBufferPointer&lt;Swift.UInt8&gt;) -&gt; () in Swift._assertionFailure(_: Swift.StaticString, _: Swift.StaticString, file: Swift.StaticString, line: Swift.UInt, flags: Swift.UInt32) -&gt; Swift.Never + 387
      frame #3: 0x00007fff3074097c libswiftCore.dylib`closure #1 (Swift.UnsafeBufferPointer&lt;Swift.UInt8&gt;) -&gt; () in closure #1 (Swift.UnsafeBufferPointer&lt;Swift.UInt8&gt;) -&gt; () in Swift._assertionFailure(_: Swift.StaticString, _: Swift.StaticString, file: Swift.StaticString, line: Swift.UInt, flags: Swift.UInt32) -&gt; Swift.Never + 300
      frame #4: 0x00007fff30740655 libswiftCore.dylib`closure #1 (Swift.UnsafeBufferPointer&lt;Swift.UInt8&gt;) -&gt; () in Swift._assertionFailure(_: Swift.StaticString, _: Swift.StaticString, file: Swift.StaticString, line: Swift.UInt, flags: Swift.UInt32) -&gt; Swift.Never + 117
      frame #5: 0x00007fff307402aa libswiftCore.dylib`Swift._assertionFailure(_: Swift.StaticString, _: Swift.StaticString, file: Swift.StaticString, line: Swift.UInt, flags: Swift.UInt32) -&gt; Swift.Never + 314
      frame #6: 0x00007fff3074053c libswiftCore.dylib`Swift._fatalErrorMessage(_: Swift.StaticString, _: Swift.StaticString, file: Swift.StaticString, line: Swift.UInt, flags: Swift.UInt32) -&gt; Swift.Never + 44
      frame #7: 0x00007fff309a431d libswiftCore.dylib`function signature specialization &lt;Arg[1] = Dead&gt; of generic specialization &lt;Swift.UInt8&gt; of Swift.UnsafeBufferPointer.init(rebasing: Swift.Slice&lt;Swift.UnsafeBufferPointer&lt;τ_0_0&gt;&gt;) -&gt; Swift.UnsafeBufferPointer&lt;τ_0_0&gt; + 109
      frame #8: 0x00007fff308ad674 libswiftCore.dylib`Swift._StringGuts.append(Swift._StringGutsSlice) -&gt; () + 500
      frame #9: 0x00007fff3084fc0a libswiftCore.dylib`Swift.Character.write&lt;τ_0_0 where τ_0_0: Swift.TextOutputStream&gt;(to: inout τ_0_0) -&gt; () + 26
      frame #10: 0x00007fff30735d28 libswiftCore.dylib`Swift.DefaultStringInterpolation.appendInterpolation&lt;τ_0_0 where τ_0_0: Swift.CustomStringConvertible, τ_0_0: Swift.TextOutputStreamable&gt;(τ_0_0) -&gt; () + 40
    * frame #11: 0x00000001051e9e45 MY_APP`static UtilityManager.clientIdFor(basicToken=&quot;MY_BASIC_TOKEN==&quot;, self=MY_APP.UtilityManager) at UtilityManager.swift:213:36
  </code></pre>
  <p>If I do a <code>po</code> in console, the same line of code works!</p>
  <pre><code>po &quot;CID: \(String(describing: Singleton.shared.settings.userName ?? &quot;NA&quot;))&quot;
  &quot;CID: NA&quot; /// &lt;- Here it works and prints NA in console.
  </code></pre>
  <p>I guess by this point in time singleton is not initialized which is why the userName is <code>nil</code></p>
  <p>Any idea on why the app is crashing?</p>