8

I can't quite figure out what I have seen referred to as an Objective-C "class continuation". Is this / are these…

  1. Ivar(s) declared in the @implementation (.m) file?
  2. Another name for a class category? (Unlikely, ASFAIK categories cannot have Ivars, period)
  3. Another name for a class extension?
  4. Something else?

That said...

  1. What is the scope, lifetime, and usage case for such a thing?
  2. Is this an ARC-specific "feature"?
  3. Are there specific runtime, or other requirements for their use?
  4. Is this an appropriate place to create an @property, as well? And why would this be a better place for setting ivars or properties than, say, the @interface file / declaration?
  5. Why do people complicate discussions by using such specific terminology - that seems NOT to exist in any official documentation (that I could find)?

In question In Objective-C what is the difference between defining something (say a property) in the header file as opposed to the .m file? the discussion touches on this issue, but sort of just clouds the issue further - or at least fails to provide a canonical reference / definition of the term… hence this question.

Community
  • 1
  • 1
Alex Gray
  • 16,007
  • 9
  • 96
  • 118
  • From a comment in the linked question.. "The correct term for an 'anonymous category' is a 'class continuation'. It is different from categories in that it can contain additional fields." or some other questions [here](http://stackoverflow.com/questions/172598/best-way-to-define-private-methods-for-a-class-in-objective-c) or [here](http://stackoverflow.com/questions/827242/can-i-create-properties-with-a-public-getter-and-private-setter) – Alex Gray Apr 24 '12 at 16:15
  • The quote is actually a very bad description of class extensions. – Nikolai Ruhe Apr 24 '12 at 18:00

1 Answers1

7

A continuation class is what Apple calls a class extension. I have seen clang call them "continuation class" and gcc uses "class continuation".

Compile this in clang or gcc:

@interface Foo : NSObject
@property int a;
@end

@interface Foo()
@property float a;
@end

... and you will get errors with the funny names.

To answer the rest of your question:

  1. What is the scope, lifetime, and usage case for such a thing?

Extensions are used to declare the private interface for a class. You can also use it to redeclare (refine) public property declarations.

  1. Is this an ARC-specific "feature"?

NO.

  1. Are there specific runtime, or other requirements for their use?

Class Extensions are a compile time concept and do not require a special runtime. Of course they do require a compiler that supports them (both clang and gcc do in current versions).

  1. Is this an appropriate place to create an @property, as well? And why would this be a better place for setting ivars or properties than, say, the @interface file / declaration?

YES. Because you might want to have private properties.

  1. Why do people complicate discussions by using such specific terminology - that seems NOT to exist in any official documentation (that I could find)?

Well, you know... I'd also prefer if the whole world spoke English, but for the time being I'm happy with the fact that I had to learn it in school.

mkirk
  • 3,965
  • 1
  • 26
  • 37
Nikolai Ruhe
  • 81,520
  • 17
  • 180
  • 200
  • before the non-fragile abi (ie. 32 bit) the extension is much more of a normal category and can not be used to declare private ivars... – Grady Player Apr 24 '12 at 16:30
  • 1
    Declaring private ivars is of course dependent on the Objective-C 2.0's runtime, whether they are declared in the class extension or the @implementation. Putting them in the class extension is only supported in clang. The features of class extensions are not touched by this. – Nikolai Ruhe Apr 24 '12 at 16:37
  • 1
    I find comparing class extensions with categories is not helpful. While the syntax is similar they serve a different purpose and have other compile time and run time characteristics. – Nikolai Ruhe Apr 24 '12 at 16:42
  • 1
    ok, that doesn't change the fact that you cant declare an ivar in an extension on 32-bit mac, clang or no clang. you also can't synthesize your backing iVar for properties... because the runtime doesn't support it. – Grady Player Apr 24 '12 at 16:51
  • 1
    Please file a bug against the misuse of the name in the compiler. Note that "class extensions" were briefly called "class continuations" during development. – bbum Apr 24 '12 at 17:14
  • In what case do you want to redeclare a public property as specified in (1)? – Boon Dec 27 '13 at 23:49
  • @Boon The question is not about code but about terminology and I needed a snippet to provoke the compiler to emit an error that refers to the class extension. – Nikolai Ruhe Dec 28 '13 at 05:55
  • @NikolaiRuhe You mentioned in (1) that you can also use it to redeclare public property declaration. My question is - why or when will there be such a need? – Boon Dec 28 '13 at 13:52
  • @Boon Oh, sorry, I misread your question. Redeclaring a property can make sense in several scenarios. It's most often used when a property is publicly `readonly` and privately `readwrite`. In this case a setter will be generated but it will be only accessible in the `@implementation`. Other cases include a publicly immutable collection that is privately mutable. – Nikolai Ruhe Dec 28 '13 at 19:09
  • Update the official doc address for [class extension](https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/CustomizingExistingClasses/CustomizingExistingClasses.html#//apple_ref/doc/uid/TP40011210-CH6-SW3) – Azu Feb 03 '15 at 06:07