Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Xcode, how do I know which class implement a protocol?

I am wondering in Xcode, can I quickly know which classes implement some protocols?

I don't want to use search, but want to quickly identify such classes.

like image 680
Adam Lee Avatar asked Aug 08 '13 06:08

Adam Lee


People also ask

What's the difference between a protocol and a class in Swift?

You can create objects from classes, whereas protocols are just type definitions. Try to think of protocols as being abstract definitions, whereas classes and structs are real things you can create.

How many protocols can Swift class adopt?

Since classes, structures and, enums can conform to more than one protocol, they can take the default implementation of multiple protocols.

What is a protocol in Xcode?

A protocol defines a blueprint of methods, properties, and other requirements that suit a particular task or piece of functionality. The protocol can then be adopted by a class, structure, or enumeration to provide an actual implementation of those requirements.

How do you ensure the adoption of a protocol only by reference type?

You can limit protocol adoption to class types (and not structures or enumerations) by adding the AnyObject or class protocol to a protocol's inheritance list.


2 Answers

You can use the Assistant Editor:

  • Open the protocol that you are interested in, place the cursor inside the @protocol definition
  • Open the Assistant Edior (command-option-return) and from the drop down menu in the upper left, select Protocols:

enter image description here

The classes that implement the protocol will be in the list.

Edited to add info from Rob's comment:

Note that this only finds classes that, either in their public header or in their implementation declare that they conform to the protocol. If conformance to the protocol is declared somewhere else, those classes will not show up.

Say you have a protocol defined somewhere as

@protocol MyProtocol <NSObject>
…
@end

A class with a public header MyClass.h like this will show up:

@interface MyClass : NSObject <MyProtocol>

Also, a class that is extended in the .m file like this will show up

@interface MyObject () <SomeProtocol>
…
@end

@implementation MyObject 
…
@end

A class extension MyClass_Extension.h like this will not show up:

@interface MyObject (Extension) <SomeProtocol>
…
@end
like image 76
Sebastian Avatar answered Oct 10 '22 08:10

Sebastian


in particular class in which protocol implement just press 'command btn + right click of mouse' on protocol method..

it will show list of all class with implementation of this method. and you can jump on that class by just click on it...

like image 30
Dafda Avatar answered Oct 10 '22 10:10

Dafda