类扩展与头文件 [英] Class Extension vs Header file

查看:111
本文介绍了类扩展与头文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是类扩展名和头文件?有什么区别?在头文件中声明属性/方法与在类扩展中声明属性/方法有什么区别?我对Objective-C完全陌生,所以初学术语会有所帮助:)

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 :)

提前谢谢!

推荐答案

顾名思义,它们扩展了该类.类的延续是另一个名字.类扩展通常用于声明私有方法和属性.您希望类扩展名对@implementation可见,而不是在头文件中(即,您希望类扩展名和@implementation在MONClass.m中).

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.

示例:在类扩展名中声明的属性对于#import标头的客户端将不可见/不可访问,但是由于@implementation可以看到类扩展名的声明,因此@implementation可以使用它

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.

因此,这可以用于模拟您的ivars和方法的受限访问.这很有用,因为不能将ObjC方法和属性指定为私有/受保护/公共(例如,使用@public).

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).

类扩展与类别不同,主要是因为扩展可以声明存储空间(例如,将产生后缀ivars的属性).

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

这篇关于类扩展与头文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆