Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
439 views
in Technique[技术] by (71.8m points)

objective c - How can I retrieve Swift "header" files for Cocoa APIs?

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!
}

Is there an alternate way to make Xcode generate this Swift version? Preferably from the command line?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The Swift REPL includes a helper :print_decl <name> - print the AST representation of the named declarations

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
: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
: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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...