The way I know I can view the automatically-translated Swift versions of Cocoa APIs is by command-clicking a Cocoa type in Xcode. For example, here's what is generated for UITableViewController:
我知道我可以查看自动翻译的Swift版本的Cocoa API的方法是通过命令单击Xcode中的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!
}
Is there an alternate way to make Xcode generate this Swift version? Preferably from the command line?
是否有另一种方法可以让Xcode生成这个Swift版本?最好是从命令行?
3 个解决方案
#1
13
The Swift REPL includes a helper :print_decl <name> - print the AST representation of the named declarations
Swift REPL包含一个帮助器:print_decl
This blog post explains how you can write a simple script to help you use this to generate documentation for a specific type. I updated the script to allow using either OS X
此博客文章介绍了如何编写一个简单的脚本来帮助您使用它来生成特定类型的文档。我更新了脚本以允许使用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
Note 1: The script defaults to using the iOS SDK. If you want to use the OS X SDK use the "--osx" option as the first parameter
注1:该脚本默认使用iOS SDK。如果要使用OS X SDK,请使用“--osx”选项作为第一个参数
Note 2: I prefer to leave out the file output that is part of the blog post so that I can use this script in other ways than just file generation
注意2:我更喜欢省略作为博客文章一部分的文件输出,以便我可以通过其他方式使用此脚本,而不仅仅是文件生成
#2
5
It turns out the modern (non-"deprecated") REPL has a way of doing this too. Rather than :print_module
, you can use :type lookup
:
事实证明,现代(非“弃用”)REPL也有办法做到这一点。而不是:print_module,您可以使用:type lookup:
echo -e "import CoreGraphics\n:type lookup CoreGraphics" | swift
#3
4
As noted in this question, the "headers" are generated from the compiled Swift modules. These are located at /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/iphoneos/
where there are both swiftdoc
and swiftmodule
files, as well as the dylib
s that contain the actual libraries.
如本问题所述,“头”是从已编译的Swift模块生成的。它们位于/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/iphoneos/,其中包含swiftdoc和swiftmodule文件,以及包含实际库的dylib。
There don't seem to be any tools to parse these at the time being. class-dump
and otool
are of no use. After some investigation, it seems Xcode communicates with SourceKitService (located at ...XcodeDefault.xctoolchain/usr/lib/sourcekitd.framework/XPCServices/SourceKitService.xpc
) via XPC in order to get these headers — the best approach would probably be to try and duplicate those messages for yourself. Creating a DTrace instrument in Instruments, you can see a little bit of what's going on (this is when I ⌘-clicked on CIFilter):
目前似乎没有任何工具可以解析这些问题。 class-dump和otool是没用的。经过一番调查后,似乎Xcode通过XPC与SourceKitService(位于... XcodeDefault.xctoolchain / usr / lib / sourcekitd.framework / XPCServices / SourceKitService.xpc)进行通信以获取这些标题 - 最好的方法可能是尝试为自己复制这些消息。在Instruments中创建DTrace工具,您可以看到一些正在发生的事情(这是当我点击CIFilter时):
And with otool
we can see that SourceKitService has symbols such as SwiftInterfaceGenContext
and swift::ide::SyntaxModelWalker
which are probably related to all this. But it would take a lot of work to suss out exactly what messages are being passed and how you could get the headers yourself. Probably not worth it!
使用otool,我们可以看到SourceKitService具有SwiftInterfaceGenContext和swift :: ide :: SyntaxModelWalker等符号,这些符号可能与所有这些相关。但是要确切地说明正在传递的消息以及如何自己获取标题需要花费大量的工作。可能不值得!
Edit: drewag's answer above shows that swift -integrated-repl
's :print_decl
command can produce the headers.
编辑:上面的drewag的回答显示swift -integrated-repl的:print_decl命令可以产生标题。
#1
13
The Swift REPL includes a helper :print_decl <name> - print the AST representation of the named declarations
Swift REPL包含一个帮助器:print_decl
This blog post explains how you can write a simple script to help you use this to generate documentation for a specific type. I updated the script to allow using either OS X
此博客文章介绍了如何编写一个简单的脚本来帮助您使用它来生成特定类型的文档。我更新了脚本以允许使用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
Note 1: The script defaults to using the iOS SDK. If you want to use the OS X SDK use the "--osx" option as the first parameter
注1:该脚本默认使用iOS SDK。如果要使用OS X SDK,请使用“--osx”选项作为第一个参数
Note 2: I prefer to leave out the file output that is part of the blog post so that I can use this script in other ways than just file generation
注意2:我更喜欢省略作为博客文章一部分的文件输出,以便我可以通过其他方式使用此脚本,而不仅仅是文件生成
#2
5
It turns out the modern (non-"deprecated") REPL has a way of doing this too. Rather than :print_module
, you can use :type lookup
:
事实证明,现代(非“弃用”)REPL也有办法做到这一点。而不是:print_module,您可以使用:type lookup:
echo -e "import CoreGraphics\n:type lookup CoreGraphics" | swift
#3
4
As noted in this question, the "headers" are generated from the compiled Swift modules. These are located at /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/iphoneos/
where there are both swiftdoc
and swiftmodule
files, as well as the dylib
s that contain the actual libraries.
如本问题所述,“头”是从已编译的Swift模块生成的。它们位于/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/iphoneos/,其中包含swiftdoc和swiftmodule文件,以及包含实际库的dylib。
There don't seem to be any tools to parse these at the time being. class-dump
and otool
are of no use. After some investigation, it seems Xcode communicates with SourceKitService (located at ...XcodeDefault.xctoolchain/usr/lib/sourcekitd.framework/XPCServices/SourceKitService.xpc
) via XPC in order to get these headers — the best approach would probably be to try and duplicate those messages for yourself. Creating a DTrace instrument in Instruments, you can see a little bit of what's going on (this is when I ⌘-clicked on CIFilter):
目前似乎没有任何工具可以解析这些问题。 class-dump和otool是没用的。经过一番调查后,似乎Xcode通过XPC与SourceKitService(位于... XcodeDefault.xctoolchain / usr / lib / sourcekitd.framework / XPCServices / SourceKitService.xpc)进行通信以获取这些标题 - 最好的方法可能是尝试为自己复制这些消息。在Instruments中创建DTrace工具,您可以看到一些正在发生的事情(这是当我点击CIFilter时):
And with otool
we can see that SourceKitService has symbols such as SwiftInterfaceGenContext
and swift::ide::SyntaxModelWalker
which are probably related to all this. But it would take a lot of work to suss out exactly what messages are being passed and how you could get the headers yourself. Probably not worth it!
使用otool,我们可以看到SourceKitService具有SwiftInterfaceGenContext和swift :: ide :: SyntaxModelWalker等符号,这些符号可能与所有这些相关。但是要确切地说明正在传递的消息以及如何自己获取标题需要花费大量的工作。可能不值得!
Edit: drewag's answer above shows that swift -integrated-repl
's :print_decl
command can produce the headers.
编辑:上面的drewag的回答显示swift -integrated-repl的:print_decl命令可以产生标题。