Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make Xcode automatically conform to a protocol

Tags:

When I use a prototype table view, I always have to conform to the protocol TableViewDataSource. I always forget what methods I need to implement, so I have to look at the source of the protocol every time. This is really time consuming.

I think Xcode must have a feature that automatically implements the needed methods for you, right? Just like IntelliJ IDEA, Eclipse, and Visual Studio.

I want to know where can I find this feature. If there's isn't, is there a workaround for this? At least I don't have to open the source code of the protocol each time I conform to it.

If you don't understand what I mean, here's some code:

I have a protocol

protocol Hello {     func doStuff () } 

When I conform to it,

class MyClass: Hello {  } 

I often don't remember the names of the methods that I need to implement. If Xcode has a feature that turns the above code, into this:

class MyClass: Hello {     func doStuff () {         code     } } 

Now you understand what I mean? I just want to ask where to find such a feature.

like image 558
Sweeper Avatar asked Jan 02 '16 09:01

Sweeper


People also ask

Is it possible to prevent the adoption of a protocol by a struct?

A protocol defines a blueprint of methods, properties, and other requirements. The protocol can then be adopted by a class, structure, or enumeration to provide an actual implementation of those requirements. But there would be a time when you want to restrict protocols to be adopted by a specific class.

How do I add a protocol in Swift?

To create a protocol, use the protocol keyword followed by the name you want and defined by the curly braces. Protocols can be of 2 types: read-only/read-write. Read-only means you can only get the variable, but you cannot set it. Read-write means you can both set and get properties.

What is conform in Swift?

Any type that satisfies the requirements of a protocol is said to conform to that protocol. In addition to specifying requirements that conforming types must implement, you can extend a protocol to implement some of these requirements or to implement additional functionality that conforming types can take advantage of.

How do you use protocols?

Protocol is used to specify particular class type property or instance property. It just specifies the type or instance property alone rather than specifying whether it is a stored or computed property. Also, it is used to specify whether the property is 'gettable' or 'settable'.


2 Answers

Xcode 9 : you can add missing Protocol Requirements by add new shortcut to your Key Bindings Set

  1. From the top Xcode menu (top left) choose Preferences.
  2. Choose Key Binding.
  3. Search about protocol. you will find Command called Refactor -> Add Missing Protocol Requirements
  4. Press on Key column then add your shortcut. ex: cmd + shift + M

Now you can add missing Protocol Requirements by clicking on class name name (or his extension) then press your shortcut

enter image description here

like image 92
Amjad Tubasi Avatar answered Nov 10 '22 13:11

Amjad Tubasi


Well if i understood your problem then here is a workaround:

try to define methods with protocol as prefix like here hello then you'll not have to remember the methods just start typing protocol name and XCODE will prompt you with all available methods see here:

enter image description here

And if you want autocomplete protocol try Snippets

like image 24
Hamza Ansari Avatar answered Nov 10 '22 11:11

Hamza Ansari