为什么 Apple 的文档只显示属性和方法签名而不显示实际代码?

Why does Apple's documentation only show the properties and method signatures but not the actual code?

我正在研究 Apple 的文档,想了解 类(例如 UIApplication)如何在后台执行它们的操作,但是如果您命令单击 "UIApplication",Xcode 只会显示UIApplication 的属性和方法签名,但不是方法中的实际代码。我认为这将是有价值的信息,可以让我们了解我们是否可以看到 Apple 提供的 类 内部发生了什么,但为什么我们无法了解或看到它?

例如,如果您命令单击 UIApplication,将显示以下内容:

public class UIApplication : UIResponder {

    public class func sharedApplication() -> UIApplication

    unowned(unsafe) public var delegate: UIApplicationDelegate?

    public func beginIgnoringInteractionEvents() // nested. set should be set during animations & transitions to ignore touch and other events
    public func endIgnoringInteractionEvents()
    public func isIgnoringInteractionEvents() -> Bool // returns YES if we are at least one deep in ignoring events

    public var idleTimerDisabled: Bool // default is NO

    public func openURL(url: NSURL) -> Bool
    @available(iOS 3.0, *)
    public func canOpenURL(url: NSURL) -> Bool

    public func sendEvent(event: UIEvent)

    public var keyWindow: UIWindow? { get }
    public var windows: [UIWindow] { get }

    public func sendAction(action: Selector, to target: AnyObject?, from sender: AnyObject?, forEvent event: UIEvent?) -> Bool

    public var networkActivityIndicatorVisible: Bool // showing network spinning gear in status bar. default is NO

    // default is UIStatusBarStyleDefault

    // The system only calls this method if the application delegate has not
    // implemented the delegate equivalent. It returns the orientations specified by
    // the application's info.plist. If no supported interface orientations were
    // specified it will return UIInterfaceOrientationMaskAll on an iPad and
    // UIInterfaceOrientationMaskAllButUpsideDown on a phone.  The return value
    // should be one of the UIInterfaceOrientationMask values which indicates the
    // orientations supported by this application.
    @available(iOS 6.0, *)
    public func supportedInterfaceOrientationsForWindow(window: UIWindow?) -> UIInterfaceOrientationMask

    public var statusBarOrientationAnimationDuration: NSTimeInterval { get } // Returns the animation duration for the status bar during a 90 degree orientation change.  It should be doubled for a 180 degree orientation change.
    public var statusBarFrame: CGRect { get } // returns CGRectZero if the status bar is hidden

    public var applicationIconBadgeNumber: Int // set to 0 to hide. default is 0. In iOS 8.0 and later, your application must register for user notifications using -[UIApplication registerUserNotificationSettings:] before being able to set the icon badge.

    @available(iOS 3.0, *)
    public var applicationSupportsShakeToEdit: Bool

    @available(iOS 4.0, *)
    public var applicationState: UIApplicationState { get }
    @available(iOS 4.0, *)
    public var backgroundTimeRemaining: NSTimeInterval { get }

    @available(iOS 4.0, *)
    public func beginBackgroundTaskWithExpirationHandler(handler: (() -> Void)?) -> UIBackgroundTaskIdentifier
    @available(iOS 7.0, *)
    public func beginBackgroundTaskWithName(taskName: String?, expirationHandler handler: (() -> Void)?) -> UIBackgroundTaskIdentifier
    @available(iOS 4.0, *)
    public func endBackgroundTask(identifier: UIBackgroundTaskIdentifier)

    /*! The system guarantees that it will not wake up your application for a background fetch more
        frequently than the interval provided. Set to UIApplicationBackgroundFetchIntervalMinimum to be
        woken as frequently as the system desires, or to UIApplicationBackgroundFetchIntervalNever (the
        default) to never be woken for a background fetch.

        This setter will have no effect unless your application has the "fetch" 
        UIBackgroundMode. See the UIApplicationDelegate method
        `application:performFetchWithCompletionHandler:` for more. */
    @available(iOS 7.0, *)
    public func setMinimumBackgroundFetchInterval(minimumBackgroundFetchInterval: NSTimeInterval)

    /*! When background refresh is available for an application, it may launched or resumed in the background to handle significant
        location changes, remote notifications, background fetches, etc. Observe UIApplicationBackgroundRefreshStatusDidChangeNotification to
        be notified of changes. */
    @available(iOS 7.0, *)
    public var backgroundRefreshStatus: UIBackgroundRefreshStatus { get }

    @available(iOS 4.0, *)
    public var protectedDataAvailable: Bool { get }

    @available(iOS 5.0, *)
    public var userInterfaceLayoutDirection: UIUserInterfaceLayoutDirection { get }

    // Return the size category
    @available(iOS 7.0, *)
    public var preferredContentSizeCategory: String { get }
}

这是向某人提供您的产品(在本例中为 Apple 的 SDK)API 时的常见做法。实现细节由创建者决定,您不需要知道内部是如何工作的。您只需要知道可以使用他们的产品的工具,这些工具称为 API。

API on wiki

实施本身可以而且经常发生变化,API 的设计方式使这些变化不会中断或破坏您的应用程序。 查看背后的代码可能会导致您做出不正确的假设 and/or 错误做法。更不用说它通常是专有的,并不是真正为 public 眼睛设计的。

编辑:

您不关心实现,因为您不是编写它的人。 Apple 创建实现,您只需使用他们在幕后制作的任何东西。

以beginIgnoringInteractionEvents方法为例。你不知道调用它时到底发生了什么。您所知道的是,一旦调用它,您将停止获取交互事件。如果通过简单地将一些内部 checkForEvents 属性 设置为 false 或者他们从处理程序列表中删除处理程序或者他们重新创建整个视图层次结构由他们决定,那么这对您来说是完全透明的。你不需要关心内部结构,你只需要知道 method/property 是做什么的,这取决于 Apple 来确保它信守诺言(你偶尔会感到失望,当你遇到行为错误)