[英]Swift Complex type in parameter
In order to be a bit more clear I am looking for a solution for the user to pass a class with a specific subclass and protocol, ie a class that inherits a viewController and delegate protocol. 为了更清楚一点,我正在寻找一种解决方案,供用户传递带有特定子类和协议的类,即,一个继承viewController和委托协议的类。 I know its possible to create a protocol list but cannot find a solution that works correctly.
我知道可以创建协议列表,但是找不到正确运行的解决方案。 Currently I use a initializer and use the viewcontroller as a parameter and check for delegate inside the function but I would rather if I can have these types in the parameter instead.
目前,我使用初始化程序并将viewcontroller用作参数,并在函数内检查委托,但我希望可以在参数中使用这些类型。
EDIT: Looking to do something similar to this 编辑:希望做与此类似的事情
Protocol prot:Delegate{}
class cla:ViewController{}
class impl{
init(mainUI: cla, prot){
do things
}
}
That way back in the main view controller I can do something like this
class viewController: cla, prot{
var view:impl
override func loadView() {
//Attach to Screen
view = impl(mainUI: self);
}
}
Their is a bit more happening but that's the only part thats really relevant. 他们还有更多的事情发生,但这是唯一真正相关的部分。 Currently I use a initializer to just fail if the class doesn't inherit the correct protocols
当前,如果类未继承正确的协议,我将使用初始化程序来使其失败
You could create a dummy type that represents your requirements. 您可以创建一个虚拟类型来代表您的需求。 A typealias doesn't work in this case, but this might:
Typealias在这种情况下不起作用,但这可能是:
class DummyClass: MyViewController, MyDelegateProtocol {}
func needsRequiredClass(input: DummyClass) {...}
With MyViewController
and MyDelegateProtocol
being the superclass and delegate you mentioned. MyViewController
和MyDelegateProtocol
是您提到的超类和委托。 The DummyClass
would have an empty implementation. DummyClass
将有一个空的实现。
Then, make your specific classes sub classes of DummyClass
: 然后,使您的特定类成为
DummyClass
子类:
class TestClass: DummyClass {...}
And you can pass in that new class: 您可以通过该新课程:
let test = TestClass()
self.needsRequiredClass(test)
You're asking the wrong question. 您问错了问题。 In other words trying to shoe-horn in a serious design mistake.
换句话说,尝试严重设计错误。
A view should not know that its delegate is a UIViewController or a subclass. 视图不应该知道,它的代表是一个UIViewController或一个子类。
A delegate should be any class that obeys (adopts) a specific delegate protocol. 委托应该是遵守(采用)特定委托协议的任何类。
A view should only delegate specific responsibilities. 视图仅应委派特定的职责。 Each of those responsibilities must be described by a protocol method or property.
这些责任中的每一项都必须由协议方法或属性来描述。
If you explain what your issue is in more detail (why you think you need direct access to the entire definition of a UIViewController
within a UIView
), we can help you avoid this mistake. 如果您更详细地说明问题(为什么您认为需要直接访问
UIView
中的UIViewController
的整个定义),我们可以帮助您避免此错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.