0

I would like to define a C struct-like in my Objective-C header file which includes _ivars only, however, since this a header file only there would be no corresponding @implementation. Is that even possible? (I also don't want to force the header file includers to add @implementation since this is a simple descriptor definition)

I would like it to be @interface definition so users who like to extend it and add more data members to it could do so (again, only _ivars). However, other suggestions might work if you think of something.

sramij
  • 4,775
  • 5
  • 33
  • 55

1 Answers1

2

Yes, there has to be an @implementation declared for the class that is compiled by the compiler to cause the class to be realized at runtime (including the storage backing the @property declarations.

Once compiled, that class cannot extended with additional @properties that are automatically backed (it can be extended via categories, but you're on your own for storage and categorical extensions of classes is generally not recommended anyway).

bbum
  • 162,346
  • 23
  • 271
  • 359
  • Thanks, I was hopping that I was missing something and that I would still be able to do it without "implementation" in case it is purely a C-struct style but wrapped up in an "interface". (by the way, I don't mind extending with categories) – sramij Oct 17 '17 at 20:12
  • You can add properties in a subclass. – Willeke Oct 17 '17 at 20:38
  • @Willeke The problem with using a class (aka interface) is that I will need to add an implementation for it, and I am trying to avoid that since I am planning to have a header file only. – sramij Oct 17 '17 at 21:24
  • A class isn't an interface. Is a category header only? – Willeke Oct 17 '17 at 23:53
  • I am writing a header only; I am not writing a .m file. I would like this structure I am proposing to be used when including the header the header w/o a corresponding .m implementation in case no extensions are required. If the "includers" want to extend it, then yeh, they would need to add a category into there .m file. – sramij Oct 17 '17 at 23:58
  • Even a structure has to be allocated somewhere. By using a class (with @implementation), the instantiation of the class takes care of creating the backing store for your "structure" members. – bbum Oct 18 '17 at 20:15