如何检索Cocoa API的Swift“头”文件?

时间:2014-07-22 12:29:09

标签: objective-c xcode cocoa swift

我知道我可以查看自动翻译的Swift版本的Cocoa API的方法是通过命令单击Xco​​de中的Cocoa类型。例如,这是为UITableViewController生成的内容:

class UITableViewController : UIViewController, UITableViewDelegate, NSObjectProtocol, UIScrollViewDelegate, UITableViewDataSource {

    init(style: UITableViewStyle)

    var tableView: UITableView!
    var clearsSelectionOnViewWillAppear: Bool // defaults to YES. If YES, any selection is cleared in viewWillAppear:

    var refreshControl: UIRefreshControl!
}

是否有另一种方法可以让Xcode生成这个Swift版本?最好是从命令行?

4 个答案:

答案 0 :(得分:13)

Swift REPL包含一个助手:print_decl <name> - print the AST representation of the named declarations

这个blog post解释了如何编写一个简单的脚本来帮助您使用它来生成特定类型的文档。我更新了脚本以允许使用OS X

#! /bin/sh
# usage: <shellscript> [--osx] typename

if [ "$1" = "--osx" ] ; then
    echo "import Cocoa\n:print_decl $2" | xcrun swift -deprecated-integrated-repl
else
    sdk_path=$(echo `xcrun --show-sdk-path` | sed 's#MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk#iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator8.0.sdk#')
    echo "import UIKit\n:print_decl $1" | xcrun swift -deprecated-integrated-repl -sdk "$sdk_path"
fi

注1:该脚本默认使用iOS SDK。如果要使用OS X SDK,请使用“--osx”选项作为第一个参数

注2:我更愿意省略博客文章中的文件输出,以便我可以通过其他方式使用此脚本,而不仅仅是文件生成

答案 1 :(得分:5)

事实证明,现代(非“弃用”)REPL也有办法做到这一点。您可以使用:print_module

而不是:type lookup
echo -e "import CoreGraphics\n:type lookup CoreGraphics" | swift

答案 2 :(得分:4)

this question所述,&#34;标题&#34;从编译的Swift模块生成。它们位于/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/iphoneos/,其中包含swiftdocswiftmodule个文件,以及包含实际库的dylib

目前似乎没有任何工具可以解析这些问题。 class-dumpotool没有用。经过一些调查后,似乎Xcode通过XPC与SourceKitService(位于...XcodeDefault.xctoolchain/usr/lib/sourcekitd.framework/XPCServices/SourceKitService.xpc )进行通信以获取这些标头 - 最好的方法可能是尝试自己复制这些消息。在Instruments中创建DTrace工具,您可以看到一些正在发生的事情(这是我⌘ - 在CIFilter上点击):

dtrace

使用otool,我们可以看到SourceKitService包含SwiftInterfaceGenContextswift::ide::SyntaxModelWalker等符号,这些符号可能与此相关。但要确切地说明正在传递的消息以及如何自己获取标题需要花费大量的工作。可能不值得!

编辑:上面的回答显示,swift -integrated-repl :print_decl命令可以生成标题。

答案 3 :(得分:0)

从 Xcode 12.5 开始,接受的答案不再有效,因为不再支持 -deprecated-integrated-repl 标志。从 @jtbandesanswer 开始,以下内容适用于 Xcode 12.5:

// iOS
echo "import UIKit\n:type lookup UIKit" | swift -sdk /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS14.5.sdk -target arm64-apple-ios14.5

// macOS
echo "import AppKit\n:type lookup AppKit" | swift -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.3.sdk -target x86_64-apple-macosx11.3

// tvOS
echo "import UIKit\n:type lookup UIKit" | swift -sdk /Applications/Xcode.app/Contents/Developer/Platforms/AppleTVOS.platform/Developer/SDKs/AppleTVOS14.5.sdk -target arm64-apple-tvos14.5

// watchOS
echo "import WatchKit\n:type lookup WatchKit" | swift -sdk /Applications/Xcode.app/Contents/Developer/Platforms/WatchOS.platform/Developer/SDKs/WatchOS7.4.sdk -target arm64_32-apple-watchos7.4 

// macCatalyst
echo "import UIKit\n:type lookup UIKit" | swift -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.3.sdk -target x86_64-apple-ios14.5-macabi -Fsystem /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.3.sdk/System/iOSSupport/System/Library/Frameworks

当然,对于您在机器上安装的任何 SDK,这看起来会略有不同,但命令中的其他所有内容都相同。