title: Check NSError code in swift
body: <p>I'm trying to check the error code value in Swift, and find myself a bit confused by the new struct types and conversions. </p>
  
  <p>What I want to do is simply take an NSError object passed in a handler closure and check its code type by comparing it to a value stored in a CMError struct. In Objective-C I would simply write</p>
  
  <pre><code>[pedometer queryPedometerDataFromDate:now toDate:now withHandler:^(CMPedometerData *pedometerData, NSError *error) {
      BOOL isAuthorized = (error.code != CMErrorMotionActivityNotAuthorized);
  }];
  </code></pre>
  
  <p>In Swift, when I write what I expect to be the equivalent </p>
  
  <pre><code>pedometer.queryPedometerDataFromDate(now, toDate: now) {(data:CMPedometerData!, error:NSError!) in
      let isAuthorised:Bool = (error.code != CMErrorMotionActivityNotAuthorized)
  }
  </code></pre>
  
  <p>I get the error <em>Could not find an overload for '!=' that accepts the supplied arguments</em>. This points to a type cast error. And indeed <code>CMErrorMotionActivityNotAuthorized</code> is of type <code>CMError</code>, which is a Swift struct. And I can't seem to convert between this <code>CMError</code> type and the <code>Int</code> type that is that of <code>error.code</code>. </p>
  
  <p>So how can I check my error code? </p>
  
  <hr>
  
  <h3>Note 1</h3>
  
  <p>If I try to decompose and explicitly cast: </p>
  
  <pre><code>let errorCode:Int = (CMErrorMotionActivityNotAuthorized as Int)
  let isAuthorized:Bool = (error.code != errorCode)
  </code></pre>
  
  <p>I get the absurd error message <em>Cannot convert the expression's type 'Int' to type 'Int'</em>.</p>
  
  <hr>
  
  <h3>Note 2</h3>
  
  <p>Documentations says <code>CMError</code> is defined as</p>
  
  <pre><code>struct CMError {
      init(_ value: CUnsignedInt)
      var value: CUnsignedInt
  }
  </code></pre>
  
  <p>in Swift. In Objective-C it is defined as</p>
  
  <pre><code>typedef enum {
     CMErrorNULL = 100,
     CMErrorDeviceRequiresMovement,
     CMErrorTrueNorthNotAvailable,
     CMErrorUnknown,
     CMErrorMotionActivityNotAvailable,
     CMErrorMotionActivityNotAuthorized,
     CMErrorMotionActivityNotEntitled,
     CMErrorInvalidParameter
  } CMError;
  </code></pre>