我有这个协议(protocol):
protocol ViewType {
associatedtype T: ViewData.View
var data:T! {get set}
}
ViewData.View
是一个类
我有一个名为 TemplateLabel
的类,它继承了 UILabel
并符合 ViewType
class TemplateLabel:UILabel, ViewType {
var data: ViewData.View.Label!
}
我从 Storyboard中获取此 TemplateLabel
作为 UIView
并尝试将 UIView
转换为 ViewType
以分配它的 data
属性
let view = SB.instantiateViewControllerWithIdentifier("view_label").view
if var v = view as? ViewType { // Error
v.data = data // Error
}
但是我得到错误:
Protocol 'ViewType' can only be used as a generic constraint because it has Self or associated type requirements
Member 'data' cannot be used on value of protocol type 'ViewType'; use a generic constraint instead
最佳答案
我有一个答案给你,但那几乎是裸代码。我认为它真的在定义的上下文中很有用。
import UIKit
// Framework
/**
* Intended usage:
*
* enum AppStoryboard: BundledStoryboard {
*
* case Login
* case Main
*
* var storyboard: UIStoryboard {
* return UIStoryboard(name: "\(self)", bundle: nil)
* }
*
* }
*/
protocol BundledStoryboard {
var storyboard: UIStoryboard { get }
}
protocol StoryboardViewController {
static var bundledStoryboard: BundledStoryboard { get }
static var storyboardId: String { get }
static func instantiateFromStoryboard() -> Self
}
extension StoryboardViewController {
static var storyboardId: String { return "\(self)" }
static func instantiateFromStoryboard() -> Self {
return bundledStoryboard.storyboard.instantiateViewControllerWithIdentifier(storyboardId) as! Self
}
}
// Application specific
enum AppStoryboard: BundledStoryboard {
//case Login
case Main
var storyboard: UIStoryboard {
return UIStoryboard(name: "\(self)", bundle: nil)
}
}
extension StoryboardViewController {
static var bundledStoryboard: BundledStoryboard { return AppStoryboard.Main }
}
// View-Model relation
protocol ViewType {
associatedtype Model
func loadModel(m: Model)
}
// ViewController
final class ViewController: UIViewController, StoryboardViewController, ViewType {
override func viewDidLoad() {
super.viewDidLoad()
}
func loadModel(m: UIColor?) {
view.backgroundColor = m // Strange example, yeah.
}
}
// Then somewhere...
let vc = ViewController.instantiateFromStoryboard()
vc.loadModel(.redColor())
我认为您在这里真的不需要任何动态解决方案。您必须知道您正在实例化什么以及它可以接收哪些数据。
关于ios - Protocol 只能用作泛型约束,因为它有 Self 或关联类型要求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36795010/