-1

I have the following code where I want to check for the type of error instance.

if let error = error {
    // error is Error
    if error is CMErrorMotionActivityNotAuthorized {

    }                        
}

It is giving me error that CMErrorMotionActivityNotAuthorized is not a type. How can I check that if the error is of type CMErrorMotionActivityNotAuthorized

Mojtaba Hosseini
  • 95,414
  • 31
  • 268
  • 278
john doe
  • 9,220
  • 23
  • 91
  • 167
  • Is this what you are looking for: https://developer.apple.com/documentation/swift/2885064-type –  Oct 03 '19 at 17:31
  • 1
    Possible duplicate of [How do you find out the type of an object (in Swift)?](https://stackoverflow.com/questions/24101450/how-do-you-find-out-the-type-of-an-object-in-swift) –  Oct 03 '19 at 17:32
  • 1
    `error` can never be `CMErrorMotionActivityNotAuthorized`, because `CMError` is not a Swift `Error`. Can you explain what you are trying to do? You should also provide some more code. – Sweeper Oct 03 '19 at 17:36
  • I am trying to check for pedometer error if the error is thrown due to unauthorized – john doe Oct 03 '19 at 17:37

2 Answers2

3

Try this:

if error as? CMError == CMErrorMotionActivityNotAuthorized {
    // handle the error
}
Yonat
  • 4,382
  • 2
  • 28
  • 37
0

You can either try this

But CMErrorMotionActivityNotAuthorized means that the app is not currently authorized to use motion activity support. Required: var CMErrorMotionActivityNotAuthorized: CMError { get }

To learn more on this you can check iOS - is Motion Activity Enabled in Settings > Privacy > Motion Activity on Stackoverflow , here is the link

champion-runner
  • 1,489
  • 1
  • 13
  • 26