objective-c - 如何检索 Cocoa API 的 Swift "header"文件?

标签 objective-c xcode cocoa swift

据我所知,查看自动翻译的 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/

相关文章:

objective-c - 如何以编程方式进入目标显示模式?

ios - 在 Google Maps SDK ios 中点击了哪个标记?

html - XCode,iOS : Parsing HTML encoding with hpple

cocoa - 网络服务器用 ="/path/to/file"png 回复 ?q "path bar"

objective-c - 自定义 View 中的中心按钮

ios - 如何找到 Storyboard制作的 View Controller 实例

objective-c - 如何阻止鼠标拖动事件移动整个窗口? [ cocoa ]

iphone - 开始编辑时 Textview 无法向上移动

ios - 如何使用 3rd 方分发配置文件构建 iOS 应用程序

objective-c - NSAlert - 你可以有一个自定义图标吗?