gpt4 book ai didi

Swift:检查自定义 View 是否符合协议(protocol)

转载 作者:行者123 更新时间:2023-11-30 11:20:04 24 4
gpt4 key购买 nike

我有一些符合协议(protocol)的自定义 View :

class CustomView1: UIView, MyProtocol {
...
}


class CustomView2: UIView, MyProtocol {
...
}

然后,我想通过检查 View 是否符合我的协议(protocol)来检查 View 是否是我的自定义 View ,但这不起作用:

let v = view.viewWithTag(1000)

if let _ = v as? MyProtocol {
print("this is my custom view")
}

if v.self is MyProtocol.Type {
print("this is my custom view")
}

有什么想法吗?

谢谢!

最佳答案

代码中一切正常,我认为您在 let v = view.viewWithTag(1000) 中得到了零

我在 Playground 上检查了你的代码,它工作正常。 enter image description here

关于Swift:检查自定义 View 是否符合协议(protocol),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51354562/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com