7

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.

L A
  • 966
  • 11
  • 25
tonytran
  • 1,068
  • 2
  • 14
  • 24
  • 4
    Your extension isn't public – Hamish Jun 05 '16 at 18:50
  • @originaluser2: good catch. appreciate your answer. You save me hours. – tonytran Jun 05 '16 at 18:56
  • Not technically an answer ;) Happy to help, feel free to delete the question now – unless you feel it would be useful for others in which case I *can* write an actual answer. What's important is that this question doesn't appear 'unresolved' to the outside world. – Hamish Jun 05 '16 at 18:58
  • I am learning Swift now and it will be great if you can write an actual answer so that other members (like me) can know what is going on. – tonytran Jun 05 '16 at 19:00

1 Answers1

10

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

    }
}
Hamish
  • 78,605
  • 19
  • 187
  • 280