有没有一种方法可以使用' is'用随机计算键入操作员?
我在“if”条件下收到以下错误:
“协议“RandomAccessCollection”只能用作通用约束,因为它具有 Self 或关联的类型要求”
let str = "mystring"
let v = str.utf8
if v is RandomAccessCollection {
print("YES")
} else {
print("NO")
}
出于好奇,有没有办法使用“is”类型检查运算符来检查对象/实例是否符合 RandomAccessCollection?
I'm getting the following error on the 'if' condition:
"Protocol 'RandomAccessCollection' can only be used as a generic constraint because it has Self or associated type requirements"
let str = "mystring"
let v = str.utf8
if v is RandomAccessCollection {
print("YES")
} else {
print("NO")
}
Out of curiosity, is there a way to use the "is" type check operator to check if an object/instance conforms to RandomAccessCollection?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这些类型的检查在运行时是 unswifty 。不要那样做。
一种可能的方法是方法过载,检查以编译时间进行进行检查,例如,
如果对象符合协议,则将第二种方法称为第一个方法。
These type checks at runtime are unswifty. Don't do that.
A possible way is method overloading, the check is performed at compile time for example
If the object conforms to the protocol the second method is called otherwise the first.