为什么我的Swift类的扩展在定义文件之外不可见?

时间:2021-10-11 22:30:21

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

我有一个Xcode生成的NSManagedObject类用于我的CoreData模型。

@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:

它在名为“SomeClass.swift”的文件中定义。我想扩展这个类,所以我创建了'SomeClassExtension.swift'。我像这样定义扩展:

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?

这些扩展方法可以在此定义文件中使用,但它们在其外部不可见。是什么导致了这个问题?

2 个解决方案

#1


36  

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.

听起来你的新文件(SomeClassExtension.swift)没有包含在正确的目标中。通过在Xcode中选择文件,然后打开“文件检查器”(“视图”菜单>“实用程序”>“显示文件检查器”),仔细检查文件的目标成员资格。确保在目标成员标题下检查正确的目标。

#2


1  

I don't see any public modifiers on those methods.

我没有在这些方法上看到任何公共修饰符。

#1


36  

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.

听起来你的新文件(SomeClassExtension.swift)没有包含在正确的目标中。通过在Xcode中选择文件,然后打开“文件检查器”(“视图”菜单>“实用程序”>“显示文件检查器”),仔细检查文件的目标成员资格。确保在目标成员标题下检查正确的目标。

#2


1  

I don't see any public modifiers on those methods.

我没有在这些方法上看到任何公共修饰符。