【发布时间】:2017-04-24 18:29:19
【问题描述】:
我在 Xcode 7.3.1 的 Swift 项目中编写了一个带有此签名的函数:
func DLog<T>(@autoclosure object: () -> T, _ file: String = #file, _ function: String = #function, _ line: Int = #line) {
}
编译器为此调用抱怨Generic parameter 'T' could not be inferred:
DLog({ var text = "Returning output list\n"; for outline in outlines { text = text + outline.debugDescription + "\n"; }; return text; })
当我尝试提供它抱怨Cannot explicitly specialize a generic function的类型时:
DLog<String>({ var text = "Returning output list\n"; for outline in outlines { text = text + outline.debugDescription + "\n"; }; return text; })
我尝试了其他几种方法,但编译器都没有满意。我也没有找到如何解决这个案例的提示。
如何在() => T 参数中构建文本并将其正确传递给函数?
【问题讨论】:
标签: swift xcode generics swift2