"Cannot inherit from non-open class" swift
Asked Answered
P

3

46

As of Xcode 8 beta 6 I now get the compile error "Cannot inherit from non-open class (Class)" outside of its defining module"

The class I was inheriting from was part of a separate Swift framework but my project compiled for Xcode 8 beta 5. What do I need to change to get my project to compile again?

Phanerozoic answered 16/8, 2016 at 15:53 Comment(1)
Related: #38947601Collegium
P
80

Found the answer myself.

In Swift 3 you can now mark a class as open instead of public this allows files outside of the module to subclass that class.

Simply replace public in your module class with open.

Reference here.

Phanerozoic answered 16/8, 2016 at 15:58 Comment(1)
Also documented in the Xcode 8 beta 6 Release Notes.Collegium
W
33

The class you inherit from needs to be defined as open instead of public.

A bit more context on the changes to access control in Swift 3:

Swift 2 only had 3 access levels:

  • private: entities are available only from within the source file where they are defined.
  • internal: entities are available to the entire module that includes the definition.
  • public: entities are intended for use as API, and can be accessed by any file that imports the module.

Swift 3 is adding 2 more access levels (open and fileprivate) and changing the meaning of private:

  • private: symbol visible within the current declaration only.
  • fileprivate: symbol visible within the current file.
  • internal: symbol visible within the current module or default access modifier.
  • public: symbol visible outside the current module.
  • open: for class or function to be subclassed or overridden outside the current module.
Womankind answered 22/8, 2016 at 6:57 Comment(0)
P
0

I had this error even after marking the class as open (on Xcode 14.1). The fix was a clean (Cmd + Shift + K) and rebuild.

Palma answered 18/11, 2022 at 19:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.