协议扩展,不符合协议 [英] protocol extension, does not conform to protocol
问题描述
我正在创建一个名为 MyFramework
的框架,其中包含具有一些默认行为的 LoginProtocol.swift
I am creating a framework named MyFramework
containing LoginProtocol.swift
which has some default behaviours
import UIKit
public protocol LoginProtocol {
func appBannerImage() -> UIImage?
func appLogoImage() -> UIImage?
}
extension LoginProtocol {
func appBannerImage() -> UIImage? {
return (UIImage(named: "login_new_top"))
}
func appLogoImage() -> UIImage? {
return (UIImage(named: "appLogo"))
}
}
接下来,我将添加一个新目标来创建一个名为 MyDemoApp
的演示应用程序,该应用程序使用 MyFramework
:
Next, I am adding a new target to create a demo application named MyDemoApp
which is using MyFramework
:
import UIKit
import MyFramework
class LoginViewContainer: UIViewController, LoginProtocol {
// I think I am fine with defaults method. But actually getting an error
}
目前,我收到来自编译器的错误,例如
Currently, I am getting an error from the compiler such as
type 'LoginViewContainer does not conform protocol 'LoginProtocol'
我不确定为什么会收到此消息,因为使用协议扩展,该类不需要遵守协议
I am not sure why I am getting this message because with protocol extension,the class does not need to conform the protocols
如果我能得到一些关于这个问题的建议就太好了.谢谢
It would be great if I can get some advices about this issue.Thanks
PS:这个是这些代码的链接.随便看看吧.
PS:this is a link for these codes. feel free to look at it.
推荐答案
问题是你的 extension
不是公开的——因此它在它定义的模块之外是不可见的,在这种情况下 <代码>我的框架代码>.
The problem is that your extension
isn't public – therefore it's not visible outside the module it's defined in, in this case MyFramework
.
这意味着您的视图控制器只知道LoginProtocol
定义(因为它是 公开的),而不知道默认实现.因此编译器抱怨协议方法没有被实现.
This means that your view controller only knows about the LoginProtocol
definition (as this is public), but not the default implementation. Therefore the compiler complains about the protocol methods not being implemented.
因此,解决方案是简单地公开扩展:
The solution therefore is to simply make the extension public:
public extension LoginProtocol {
func appBannerImage() -> UIImage? {
return (UIImage(named: "login_new_top"))
}
func appLogoImage() -> UIImage? {
return (UIImage(named: "appLogo"))
}
}
这篇关于协议扩展,不符合协议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!