简体   繁体   English

Swift-具有不同方法名称的多个扩展名,但仍无法正常工作

[英]Swift - multiple extension with different method name but still not working

I have two framework projects (A and B) and in both the frameworks I have a UIViewController Extension. 我有两个框架项目(A和B),并且在两个框架中都有UIViewController扩展。

  1. My extension in framework B has method "loadFromNib" 我在框架B中的扩展具有方法“ loadFromNib”
  2. In framework "B" I have classes accessing "loadFromNib" 在框架“ B”中,我有访问“ loadFromNib”的类
  3. In framework "B" I also refer the framework "A" 在框架“ B”中,我也指框架“ A”
  4. Now I have added UIViewController extension to Framework "A" but with the name "loadViewFromNib". 现在,我已将UIViewController扩展添加到框架“ A”,但名称为“ loadViewFromNib”。

Now my existing classes throw error that loadFromNib is not available and suggests me to access the loadViewFromNib method. 现在,我现有的类抛出错误,提示loadFromNib不可用,建议我访问loadViewFromNib方法。 What is that I am missing here. 我在这里想念的是什么。 Why my classes are not referring to the right extension 为什么我的课没有提到正确的扩展名

//Code from Framework B
extension UIViewController
{
    public class func loadFromNib() -> Self?
    {
        return loadNibFilePrivateMethod()
    }
    ....
}

//Code from Framework A
extension UIViewController
{
    public class func loadViewFromNib() -> Self?
    {
        return loadNibFilePrivateMethod()
    }
    ....
}

I am in the process of merging common code to Framework A from multiple other frameworks (owned by other teams) and cannot modify anything in any other frameworks. 我正在将通用代码从多个其他框架(由其他团队拥有)合并到框架A中,并且无法在任何其他框架中进行任何修改。 So I want my code to co-exist without breaking anything. 因此,我希望代码可以共存而不破坏任何内容。

Now my existing classes throw error that loadFromNib is not available 现在我现有的类抛出错误,提示loadFromNib不可用

Possibly because you are trying to use loadFromNib from outside the framework where it is declared? 可能是因为您试图从声明它的框架外部使用loadFromNib (That's what the error message ...not available means) (这就是错误消息...不可用的意思)

When you want a method/type/function to be available to other modules (think frameworks, targets) you need to declare them as public . 当您希望方法/类型/函数可用于其他模块(例如框架,目标)时,您需要将它们声明为public

So to resolve your issue, declare you method 因此,要解决您的问题,请声明您的方法

public func loadFromNib(...

and it will be available most likely. 并且很可能会提供。

Update 更新

Are you calling the methods on a viewcontroller instance or on a view? 您是在ViewController实例上还是在视图上调用方法? The name suggests ambiguity in what they are expected to do. 这个名字暗示了他们期望做什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM