我听到很多新的编程语言都是动态输入的,但是当我们说一种语言是动态类型而非静态类型时,它实际意味着什么呢?
computer-science programming-languages static-typing dynamic-typing
实际上,我希望有一个返回元类型的协议(例如:Type.Type),我可以传递给一个Class,然后当我需要时,将一个对象转换为该MetaType.我要强制转换它的原因是它将在tableView dequeue函数中使用,我想要转换为我指定的Type I.
考虑这个精简版(下面的完整版).
let anyObject: AnyObject = anything()
let aType = type.type() // aType here is A.Type
if let newType = anyObject as? aType {
print(newType)
}
// error: 'aType' is not a type
Run Code Online (Sandbox Code Playgroud)
我很困惑为什么这不是一个类型,因为我们可以去aType.init()来初始化它?
下面是完整的示例代码(可以在Playground中运行).
import UIKit
protocol P {
func type() -> A.Type
}
class A {
}
class B: A {
}
class C: A {
}
struct BData: P {
func type() -> A.Type {
return B.self
}
}
struct Foo {
let type: P …Run Code Online (Sandbox Code Playgroud) 我有一个t传递给方法的Type变量,我想在调用时将其用作通用参数IQueryable.Join,如下所示
queryResult.Join<Type1, Type2, t, Type3>( items, outerSelector, innerSelector, ( a, b) => a);
Run Code Online (Sandbox Code Playgroud)
它显然不起作用.t为了达到我的目的,我该怎么做?谢谢!