如何检索Swift“header” Cocoa API的文件? [英] How can I retrieve Swift "header" files for Cocoa APIs?
问题描述
我知道我可以查看自动翻译的Swift版本的Cocoa API是通过命令点击Xcode中的Cocoa类型。例如,下面是为UITableViewController生成的:
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:
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版本?最好从命令行?
Is there an alternate way to make Xcode generate this Swift version? Preferably from the command line?
推荐答案
Swift REPL包含一个助手:print_decl& - 打印命名声明的AST表示
The Swift REPL includes a helper :print_decl <name> - print the AST representation of the named declarations
此博客文章解释了如何编写一个简单的脚本,以帮助您使用它来生成特定类型的文档。我更新了脚本以允许使用OS X
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
#! /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选项作为第一个参数
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
注意2:作为博客文章的一部分的文件输出,以便我可以使用这个脚本以其他方式,而不只是文件生成
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
这篇关于如何检索Swift“header” Cocoa API的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!