5

I have a "MyConstants.h" file that is imported by several classes.

Inside that file I have things like:

static BOOL isIndexValid(NSInteger index) {
  return ((index >=0) && (index < 200));
}

This function is extensively used by the classes importing MyConstants.h. Even so, Xcode complains that this function and others are not used.

Why?

LinusGeffarth
  • 27,197
  • 29
  • 120
  • 174
Duck
  • 34,902
  • 47
  • 248
  • 470

2 Answers2

9

Defining a static function (or variable, for that matter) in a header file means every source file that imports that header file will get its own copy.

That is not good and is what the compiler is complaining about (not every source file references this function).

Make it static inline instead:

static inline BOOL isIndexValid(NSInteger index) {
  return ((index >=0) && (index < 200));
}
Droppy
  • 9,691
  • 1
  • 20
  • 27
  • 1
    Thanks for the tip. You were almost there. In fact the problem solved when I put `static inline`. Just `inline` made the problem worse. Thanks. I will accept it when SO let me. – Duck Jul 27 '16 at 13:55
  • BTW I thought the static word would force the compiler to create just one version of the item with the same address in memory. – Duck Jul 27 '16 at 13:56
  • 1
    @SpaceDog Only if you put it directly into the source file, not the header file. – Droppy Jul 27 '16 at 13:58
  • @SpaceDog And thanks for the update about `static inline`. Brain fart... – Droppy Jul 27 '16 at 13:59
1

Try to insert __unused between return type and function name, and it works for me on Xcode 10.2

static BOOL __unused isIndexValid(NSInteger index) {
  return ((index >=0) && (index < 200));
}

Hope it will be helpful for you.

DawnSong
  • 4,752
  • 2
  • 38
  • 38
  • Thanks for the tip about recent Xcode versions deviating from the behaviour described above, and for providing a solution. – Bill Hollings Sep 20 '19 at 18:22