Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 378 Vote(s) - 3.46 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Auto-implement Swift protocol methods in Xcode

#1
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
Reply

#2
Xcode 9, takes care of implementation of mandatory methods of Swift Datasource & Delegates.

Look at these snapshots, with example of `UICollectionViewDataSource`:

Indicating warning to implement protocol methods:

[![enter image description here][1]][1]


By clicking on 'Fix' button, it has added all mandatory methods:

[![enter image description here][2]][2]


**Alternate Option**:
<br>You can use a keyboard short key: <kbd>Ctrl</kbd> + <kbd>Option</kbd> + <kbd>Command</kbd> + <kbd>F</kbd> to fix all issues at once.

[![enter image description here][3]][3]


[1]:

[2]:

[3]:
Reply

#3
# Xcode 9

Xcode 9 now supports conforming to protocol methods via a "fix it". It will automatically fill the missing methods for you. This was announced in WWDC 2017.

[![enter image description here][1]][1]

<strike>**Update:** This doesn't seem to work for protocols defined by you, unfortunately. </strike>

[1]:
Reply

#4
**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](

[To see links please register here]

).**

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][1]

Hitting <kbd>Enter</kbd> 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 <kbd>Cmd</kbd>-click or <kbd>Option</kbd>-<kbd>Cmd</kbd>-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.

[1]:
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through