繁体   English   中英

Swift和方法原型-前向声明

[英]Swift and method prototype - forward declaration

探索Swift头文件我看到Apple使用了这种模式,尤其是以下结构的init声明没有实现。 显然init()实现是某种方式隐藏的,因为它是Apple的东西,但是我试图理解如何实现。 这仅是一个示例,但在标题中似乎是常见的行为

struct AutoreleasingUnsafePointer<T> : 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内,则会出现两个错误(类上没有主体的函数,以及扩展名上的无效重新声明)

在目标C中,这是“隐式的”,因为方法声明位于.h中,而实现位于.m中。如何在Swift中执行相同操作?

我认为解释很简单。 您在Xcode中看到的实际上不是有效的Swift代码。

这是将Obj-C标头自动转换为类似Swift的代码的结果,但是它不是可编译的Swift。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM