据我所知,查看自动翻译的 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!
}
有没有其他方法可以让 Xcode 生成这个 Swift 版本?最好从命令行?
最佳答案
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:我更喜欢省略博客文章中的文件输出,这样我就可以在文件生成之外的其他方式中使用此脚本
关于objective-c - 如何检索 Cocoa API 的 Swift "header"文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24887454/