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
393 views
in Technique[技术] by (71.8m points)

ios - Auto-implement Swift protocol methods in Xcode

I just started with the swift language and coming from java it looks great so far. But I'm really missing something:

Eclipse had this great feature that when I added "implements XYZ" it provided me a function to automatically implement the necessary function stubs. This is a huge timesaver.

Xcode doesn't seem to have this feature, or am I missing something?

I found the accessorize plugin, but this seems only to work with Objective C..

Is there a solution?

PS. I'm using Xcode 6.1.1

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

NB: This answer is true for Xcode before version 9; in Version 9 Apple added the ability to automatically fix up missing protocol methods; see Guy Daher's answer.

I just created an iOS project, and headed into my UIViewController.swift file. There I declared that it implemented UITableViewDelegate:

class ViewController: UIViewController, UITableViewDelegate {

Now when I go into the body of the class, and start typing "tab...", I see the appropriate autocompletions:

Xcode autocompletions

Hitting Enter then inserts the function stub, with the cursor placed ready to code.

That's just the way Xcode autocompletes protocols (or inherited class methods.) If you want to do all of them at once, I don't think it's possible, sadly. But you can always Cmd-click or Option-Cmd-click (which uses a secondary editor window) on the protocol name to jump to its definition, which is basically a list of all its methods you can copy and paste into your code. You'd need to tidy up a little from there, but not much.


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

...