Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is the extension of my Swift class not visible outside the defining file?

I have an Xcode-generated NSManagedObject class for my CoreData model.

@objc(SomeClass) class SomeClass : NSManagedObject { /* ... */ }

It is defined in a file named 'SomeClass.swift'. I would like to extend this class, so I created 'SomeClassExtension.swift'. I define the extension like this:

extension SomeClass {
    class func typeMethod1() {}
    func instanceMethod2() {}
}

These extension methods can be used within this defining file, but they are not visible outside of it. What is causing this issue?

like image 700
Richard Dong Avatar asked Sep 30 '14 01:09

Richard Dong


People also ask

Can we create extension of final class in Swift?

Yes, you can extend a final class.


1 Answers

It sounds like your new file (SomeClassExtension.swift) didn't get included in the correct target. Double-check the file's target membership by selecting the file in Xcode, then opening the "File Inspector" (View menu > Utilities > Show File Inspector). Make sure the correct targets are checked under the Target Membership heading.

like image 88
Nate Cook Avatar answered Oct 24 '22 10:10

Nate Cook