ios - swift 库如何隐藏其实现

标签 ios swift

在某个时间点,我们可能尝试查看苹果为 ios 开发提供的包/库的代码。例如:UIViewController、SFSafariViewController 等。当我们进入这些包/库的文件时,我们会发现类似的内容。

extension UIViewController {

    @available(iOS 5.0, *)
    open var children: [UIViewController] { get }

    @available(iOS 5.0, *)
    open func addChild(_ childController: UIViewController)


    @available(iOS 5.0, *)
    open func removeFromParent()

    @available(iOS 5.0, *)
    open func transition(from fromViewController: UIViewController, to toViewController: UIViewController, duration: TimeInterval, options: UIView.AnimationOptions = [], animations: (() -> Void)?, completion: ((Bool) -> Void)? = nil)

    @available(iOS 7.0, *)
    open var childForStatusBarStyle: UIViewController? { get }

    @available(iOS 7.0, *)
    open var childForStatusBarHidden: UIViewController? { get }

}

这只是该文件所包含内容的一部分。但这只是一个方法签名,这些方法在其实现中还有很多对用户访问隐藏的内容。

我的问题是

How does this is possible? How can we hide or provide user only the function signature to use and rest thing is hidden from user's eye?

最佳答案

UIKit是一个框架,因此它具有访问控制

来自docs.swift.org

Access control restricts access to parts of your code from code in other source files and modules. This feature enables you to hide the implementation details of your code, and to specify a preferred interface through which that code can be accessed and used.

您可以使用访问控制将框架添加到您的项目

Access Control document

Swift standard library

docs.swift.org 的 Swift Access Controls 中:

Open access and public access enable entities to be used within any source file from their defining module, and also in a source file from another module that imports the defining module. You typically use open or public access when specifying the public interface to a framework. The difference between open and public access is described below.

Internal access enables entities to be used within any source file from their defining module, but not in any source file outside of that module. You typically use internal access when defining an app’s or a framework’s internal structure.

File-private access restricts the use of an entity to its own defining source file. Use file-private access to hide the implementation details of a specific piece of functionality when those details are used within an entire file.

Private access restricts the use of an entity to the enclosing declaration, and to extensions of that declaration that are in the same file. Use private access to hide the implementation details of a specific piece of functionality when those details are used only within a single declaration.

关于ios - swift 库如何隐藏其实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57106304/

相关文章:

ios - Json 表格 View Swift

swift - “ fatal error :在展开可选值时意外发现nil”是什么意思?

ios - GIDSignIn 需要密码

ios - 无法在属性初始值设定项中使用实例成员 'dateType';属性初始值设定项在 'self' 可用之前运行

ios - 全景图像的无限或无限滚动

ios - UITextField 文本意外移动

ios - 如何比较两个 UIImage 对象

ios - swift : Data passed through unwind segue is erased before being read

ios - UIview动画中断IOS

ios - Swift 中从 UIScrollView 到其他 View 的 Segue