I'm looking to try and cast a type based on a variable. The example below fails saying "Use of undeclared type, 'myType'"
我想尝试根据变量强制转换类型。下面的例子没有说“使用未声明的类型,'myType'”
protocol Parent {}
protocol Child: Parent {}
struct Foo: Child {}
let foo: Parent = Foo()
let myType = Child.self
if let _ = foo as? myType {
print("success")
}
Is there any way to cast based on the variable myType
?
有什么方法可以基于变量myType进行转换吗?
1 个解决方案
#1
1
Having reflected, casting is not the right question to ask here. Here i'm just checking for conformance. Dynamic casting would be useless as there's no way for the static analyzer to know what to do with it if it's casting based on a variable type.
反思,铸造不是这里要问的正确问题。在这里,我只是检查一致性。动态铸造将毫无用处,因为如果基于变量类型进行铸造,静态分析器无法知道如何处理它。
#1
1
Having reflected, casting is not the right question to ask here. Here i'm just checking for conformance. Dynamic casting would be useless as there's no way for the static analyzer to know what to do with it if it's casting based on a variable type.
反思,铸造不是这里要问的正确问题。在这里,我只是检查一致性。动态铸造将毫无用处,因为如果基于变量类型进行铸造,静态分析器无法知道如何处理它。