Class Extension vs Header file [duplicate]

情到浓时终转凉″ 提交于 2019-12-12 04:57:44

问题


What exactly are class extensions and header files? What are the differences? What is the difference between declaring a property/method in a header file vs in a class extension. I am completely new to objective-c so beginner terminology would be beneficial :)

Thanks in advance!


回答1:


As the name suggests, they extend the class. A class continuation is another name. The class extension is commonly used to declare private methods and properties. You want the class extension to be visible to the @implementation, and not in the header file (i.e. you want the class extension and @implementation to be in MONClass.m).

Types and methods in the header file are generally intended to be public -- available to any client.

Example: The property declared in the class extension will not be visible/accessible to clients who #import the header, but it will be usable by the @implementation because the @implementation can see the declarations of the class extension.

So this can be used to emulate restricted access for your ivars and methods. This is useful because ObjC methods and properties cannot be specified as private/protected/public (e.g. using @public).

Class extensions differ from categories primarily because extensions may declare storage (e.g. properties which will produce backing ivars).




回答2:


A header file (.h) is where you would declare properties, methods, and protocols publicly in an existing class. You can then import this file and, of course, use it for your implementation.

An extension is another @interface in your implementation (.m) file. This extends the imported header's internal implementation, adding methods that would not be available should someone or something else import the header file associated with the class.

For more information, check out Apple's documentation on customizing existing classes




回答3:


Q: What exactly are ... header files
A: Header file - is a file, content of which compiler 'inserts' instead of #import... (#include and other similar directives) line. Header files contains public code: forward declarations of classes, enums, variables, functions and other and other.

Q: What exactly are class extensions …
A: Class Extension - is a language construct, which allows you to extends the interface to the class.

To better understand what it is you must understand what is a class category.
Category - is a language construct, which allows you to add functionality (methods only!) to existing class. Even without subclassing.
Example:
You can add new method to NSImage:

@interface NSImage(YourExtensionName)
- (CGImageRef)CGImage;
@end

A Class Extension (also known as a class continuation, or unnamed category) bears some similarity to a category, but it can only be added to a class for which you have the source code at compile time.
In class extension you can use same things you use in a usual @interface block.

Q: What are difference A: Header file uses for include to your program some ability (structures, data types, constants, functions and so one). Class extensions uses for extends existing class with some functionality. Usually, class extension is a private interface of a class. The functionality declared by a class extension are implemented in the @implementation block for the original class so you can’t, for example, declare a class extension on a framework class, such as a Cocoa or Cocoa Touch class like NSString.

Q: What is the difference between declaring a property/method in a header file vs in a class extension
A: If you declare property/method in header file, then any user of .h file can access to this property/method. Class extensions uses for declare private interface for you class.

I strongly recommend to you read Apple's Objective-C Programming Guide.



来源:https://stackoverflow.com/questions/17372257/class-extension-vs-header-file

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