3

In objective-c, you can always check a class's protocols through its public headers. But if you have a class in swift, how do you know what protocols it conforms to? Is this even possible?

(Yes, I realize Apple's documentation lists the protocols it conforms to, but that doesn't seem like a real solution, since you might be working with a private framework or have some other theoretical reason for wanting to know an object's protocols.)

Also, yes, I know you can check for a specific protocol using the technique outlined here: https://stackoverflow.com/a/37351027/18961

1

2 Answers 2

2

In Xcode, you can command-click on any Swift type to get a header-like view of its public interface, including protocol conformances added in both the original definition and in extensions.

This doesn’t show protocol conformances added by libraries outside the defining library, e.g. if you command-click String you won’t see your own extension String: MyFunkyProtocol. But I think it is essentially the header-file-like thing you’re looking for.

For the Swift standard lib, http://swiftdoc.org/ is a nice reference.

Sign up to request clarification or add additional context in comments.

2 Comments

What is the specific menu option to see this public interface? It's possible I'm just not seeing it.
Right click on text in editor, “Jump to Definition.”
1

I don't believe you can do it without leaning on the Objective-C runtime. Import ObjectiveC and use the class_copyProtocolList() function.

2 Comments

This was my understanding too, but I was hoping there was another way.
This only works for subclasses of NSObject conforming to @objc protocols.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.