Search code examples
iosswiftnamespacesfoundation

No type named 'Error' in module 'Foundation'


I have created a class named Error. Now, there's also the class Error in Foundation and I still need to be able to access that one.
What I usually do in this case is apply proper namespacing:

Foundation.Error

However, I am getting the following error message:

No type named 'Error' in module 'Foundation'

I've checked the documentation to verify that Error is actually from Foundation:

error class in foundation from the docs

What am I doing wrong?


Solution

  • The Error protocol is defined in the Swift Standard Library. You don't even need to import Foundation in order to use it:

    $ swift
    Welcome to Apple Swift version 4.2 (swiftlang-1000.11.37.1 clang-1000.11.45.1). Type :help for assistance.
      1> var e: Error?
    e: Error? = nil
      2>  
    

    The fully qualified name is therefore Swift.Error.

    The (useful) localizedDescription property however is defined in the Foundation framework, as a protocol extension method.