protocol extension, does not conform to protocol

独自空忆成欢 提交于 2019-12-23 09:59:58

问题


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"))

    }
}

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:this is a link for these codes. feel free to look at it.


回答1:


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.

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"))

    }
}


来源:https://stackoverflow.com/questions/37645540/protocol-extension-does-not-conform-to-protocol

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!