Search code examples
xcodeswiftidecode-completion

Xcode swift method completion does not promoted anything after stroke the [tab] key


Despite so much advantages of swift lang, one of the reason I did not adopt swift code is its bad code completion support with Xcode.

Here are two scenarios to better describe my question.

Scene 1:

During daily app development, I may come across quite a few protocol methods. Take the following UITableViewDataSource method

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)

for example, after typing

func tableView(tableView: UITableView,

, I will normally type [tab] key, but there are nothing to promote in swift lang.

while things go smoothly in Objective-C: enter image description here

So most of the cases, I have to jump to UITableView(wait a 1-2 secs for Xcode to generate swift header), and copy the method needed, then paste it the place I was coding.

By the way, if you [command + left] the method pasted above, Xcode has no idea what the function is, and where it is defined.

Scene 2:

To tweak animation, I need to change a method to an alternative one with more args.

//from
class func animateWithDuration(duration: NSTimeInterval, animations: () -> Void)
//to
class func animateWithDuration(duration: NSTimeInterval, animations: () -> Void, completion: ((Bool) -> Void)?)

In Objective-C, I gonna stroke [tab] before the last closing parenthesis, everything works fine. When I do this in swift, just a tab(four blanks) will appear.

My questions are:

  1. How do you accomplish the two scenes I described above?
  2. Are there some better way to do the same job for swift?(Maybe a plugin available out there, or any other helpful tips)

Solution

  • This is something I also despise while writing code in swift. There is no great way to do this, therefore you have to choose your method before you let it autocomplete.

    For both scenario 1 and 2, you would have to type the first letters of tableView and animateWithDuration respectively, and use the up and down arrows to choose your method, and hit tab to finish the entire method.

    The plus of swift is that it will automatically fill in func or class func, but after you fill in the methods, you can't add or remove parameters.

    So far I can't find an easier way to do this. You'll have to wait for apple to improve their autocomplete.