Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to cast Class object to conformance witih protocol
    primarykey
    data
    text
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. CORather than casting it to a protocol, you should change the class, itself, to just conform to that protocol (e.g. `@interface MyClass <MyProtocol>` in the .h, or `@interface MyClass () <MyProtocol>` in the .m. Then the compiler will allow autocomplete with that protocol's methods and no casting is needed. It's generally better to fix the object or class definition than it is to cast it. Also, by defining the class to conform to a particular protocol, the compiler will also warn you if you failed to implement some required methods of the protocol.
      singulars
    2. COThanks, but maybe I wasn't clear enough. cls is a class object (an object of type Class), not a class I've defined (in other words, it's passed in as [SomeClass class]). In this simple example, if arg != nil, then I know that cls is a class which implements Protocol. I just want Xcode to realise that.
      singulars
    3. COI'm very surprised you don't get compiler warnings here because, first that you're comparing a BOOL to a pointer (passing an `arg` of `nil` is the same as passing `FALSE`), and second that you're not returning anything from this method. Perhaps you're using an older version of the compiler which doesn't generate the warning, or perhaps you've turned off some warnings. Your construct makes sense if `arg` was `id` or a pointer, but not if `BOOL`. Also, you're not returning `instance`. When I compile your code, I get warnings for both of those.
      singulars
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload