gpt4 book ai didi

ios - swift 库如何隐藏其实现

转载 作者:行者123 更新时间:2023-11-29 05:35:10 24 4
gpt4 key购买 nike

在某个时间点,我们可能尝试查看苹果为 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com