探索Swift标题我看到Apple使用的这种模式,特别是以下结构的init声明没有实现.显然init()实现是以某种方式隐藏的,因为它是Apple的东西,但我试图理解如何.这只是一个示例,但它似乎是标题中的常见行为
struct AutoreleasingUnsafePointer: Equatable, LogicValue { let value: Builtin.RawPointer init(_ value: Builtin.RawPointer) // <---- how is it possible? where is the implementation? func getLogicValue() -> Bool /// Access the underlying raw memory, getting and /// setting values. var memory: T }
我知道可以声明一个协议加上一个类扩展,这样就可以从类声明中"隐藏"实现并将其移动到别处
class TestClass :TestClassProtocol { // nothing here } protocol TestClassProtocol { func someMethod() // here is the method declaration } extension TestClass { func someMethod() // here is the method implementation { println("extended method") } }
但它与我在Apple Headers中看到的不同,因为方法"声明"在"类"内,而不在"协议"内.如果我尝试将方法声明放在类TestClass中,但是,我有两个错误(类上没有body的函数,扩展名上的无效重新声明)
在Objective C中,这是"隐式的",因为方法声明在.h和.m中的实现如何在Swift中做同样的事情?