EsbenB
EsbenB

Reputation: 3406

warning on sortedArrayUsingSelector

I have the following line of code in my iPhone app:

[[sections allValues] sortedArrayUsingSelector:@selector(sortSectionsBySectionName:)];

which generates a Undeclared selector warning.

All the objects in the array implements sortSectionsBySectionName:, so everything works as expected. I would, however, like to get rid of the warning.

Is there any way to tell the compiler that, the objects will indeed implement the selector? casting or something similar?

Any suggestions would be appreciated!

Upvotes: 2

Views: 205

Answers (1)

Wain
Wain

Reputation: 119031

The method used should be publicly visible to the class using it. That generally means either:

  1. Add sortSectionsBySectionName: to the .h file of the objects in the array and #import the .h file in this controller
  2. Add a category on the objects in the array class, in the top of this controller, and define the sortSectionsBySectionName: method there

Once the compiler can see the existence of the method within the scope you're trying to use it you should be good.

Alternatively, ask the compiler to ignore it:

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wundeclared-selector"

[[sections allValues] sortedArrayUsingSelector:@selector(sortSectionsBySectionName:)];

#pragma clang diagnostic pop

but beware that this (and the category approach) could both hide issues that will cause problems at runtime...

Upvotes: 1

Related Questions