问题描述
Swift 中的匿名类是否有等效的语法或技术?只是为了澄清 Java 示例中的匿名类 - http://docs.oracle.com/javase/教程/java/javaOO/anonymousclasses.html
Is there an equivalent syntax or technique for Anonymous class in Swift?Just for clarification Anonymous class in Java example here - http://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html
谢谢!
推荐答案
据我所知,没有等效的语法.
There is no equivalent syntax, as far as I know.
关于等效技术,理论上您可以使用闭包并在其中定义结构和类.可悲的是,我无法让它在操场或项目中工作而不使其崩溃.这很可能还没有准备好在当前的测试版中使用.
Regarding equivalent techniques, theoretically you could use closures and define structs and classes inside them. Sadly, I can't get this to work in a playground or project without making it crash. Most likely this isn't ready to be used in the current beta.
有点像……
protocol SomeProtocol {
func hello()
}
let closure : () -> () = {
class NotSoAnonymousClass : SomeProtocol {
func hello() {
println("Hello")
}
}
let object = NotSoAnonymousClass()
object.hello()
}
...当前输出此错误:
...currently outputs this error:
invalid linkage type for global declaration
%swift.full_heapmetadata* @_TMdCFIv4Test7closureFT_T_iU_FT_T_L_19NotSoAnonymousClass
LLVM ERROR: Broken module found, compilation aborted!
Command /Applications/Xcode6-Beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift failed with exit code 1
这篇关于swift中的匿名类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!