MShah MShah - 2 months ago 7
iOS Question

Get values of variables defined by protocol from any VC

protocol(myProtocol):-

protocol myProtocol {
var type:String { get set }
var sub:String { get }
var msg:String? { get set }
}


Class(myVC):-

class myVC: UIViewController, myProtocol {
//Protocol Declarations
var sub = myTypes.type.rawValue
var type = myTypes.type.getType()
var msg :String?

.... }


Extension:-

extension UIViewController
{
func getData() {
if self is myProtocol {
let msg = self.msg

} }
}


Getting error at 'self.msg' saying
Value of type UIViewController has no member 'sub'


How do i go about it?
Any help is appreciated.

Answer

All you need to do is write another line of code in your extension class. I just tested it in my end and it worked for me. Here is the code I wrote -

extension UIViewController
{
    func getData() {
        if self is myProtocol {
            let x = self as! myProtocol
            let msg = x.msg
            print(x.msg)
        }
    }
}