What is an Objective-C “class continuation”?

大城市里の小女人 提交于 2019-12-18 04:53:05

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/10301661/what-is-an-objective-c-class-continuation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!