我知道我们可以通过使用AnyObject来基本上指定我们的泛型是任何引用类型: 
  
  
 
class Foo<T: AnyObject> {
    // ...
} 
 但是有没有办法指定我们的泛型应该只是值类型,不允许引用类型?
// some code for testing    
class C { } // just a simple class as an example for a reference type
var c = C()
var d: Double = 0.9 // a value type 
 解决方案1通过扩展
protocol ValueType { }
extension Double : ValueType { }
extension Int : ValueType { }
// ... all value types to be added
func printT1 <T: ValueType> (input: T) {
    println("\(input) is value")
}
printT1(d) // Does work
//printT1(c) // Does not work 
 但是如在注释中所提到的,它正在工作但不可行,因为用户定义的值类型必须实现此协议.
解决方案2通过方法签名
func printT <T: AnyObject> (input: T) {
    println("\(input) is reference")
}
func printT <T: Any> (input: T) {
    println("\(input) is value")
} 
 解决方案3通过断言
另一个解决方案可以是通过断言
func printT <T: Any> (input: T) {
    print("\(input) is " + ((T.self is AnyObject) ? "reference" : "value"))
} 
 “解决方案”4通过where子句
这将是最好的解决方案,我想.不幸的是,这是不可能的
func printT <T: Any where T: ~AnyObject > (input: T) {
    println("\(input) is value")
} 
 或类似.也许在将来的Swift版本中可能有可能.