Swift和方法原型 - 前向声明

 李金铃519 发布于 2023-01-11 19:04

探索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中做同样的事情?

撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有