从任何 VC 获取协议定义的变量值
Get values of variables defined by protocol from any VC
协议(我的协议):-
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 UIViewController
{
func getData() {
if self is myProtocol {
let msg = self.msg
} }
}
在 'self.msg' 出现错误说 Value of type UIViewController has no member 'sub'
我该怎么做?
感谢任何帮助。
根据您的示例,您需要更改您的扩展定义
extension UIViewController
作为
extension myVC
您需要做的就是在您的扩展中再写一行代码 class。我刚刚对它进行了测试,它对我有用。这是我写的代码 -
extension UIViewController
{
func getData() {
if self is myProtocol {
let x = self as! myProtocol
let msg = x.msg
print(x.msg)
}
}
}
协议(我的协议):-
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 UIViewController
{
func getData() {
if self is myProtocol {
let msg = self.msg
} }
}
在 'self.msg' 出现错误说 Value of type UIViewController has no member 'sub'
我该怎么做? 感谢任何帮助。
根据您的示例,您需要更改您的扩展定义
extension UIViewController
作为
extension myVC
您需要做的就是在您的扩展中再写一行代码 class。我刚刚对它进行了测试,它对我有用。这是我写的代码 -
extension UIViewController
{
func getData() {
if self is myProtocol {
let x = self as! myProtocol
let msg = x.msg
print(x.msg)
}
}
}