Objective-C 中 .m 文件中的 @interface
前言:博主为一枚因为工作需要正在艰难地新学 IOS 开发的程序媛,对于 Objective-C 还不甚了解,所以博文内容可能不是那么严谨,如有童鞋发现不妥之处,还望告知博主,万分感谢!
问题描述
博主今早在看一份 Objective-C 的代码时,在 SignInViewController.m 文件中看到如下代码片段:
#import "SignInViewController.h"
@interface SignInViewController ()
@property (weak, nonatomic) IBOutlet UITextField *userNameTextField;
@property (weak, nonatomic) IBOutlet UITextField *userPassTextField;
@property (weak, nonatomic) IBOutlet UIButton *signInBtn;
@property (strong, nonatomic) NSString *userName;
@property (strong, nonatomic) NSString *userPass;
@end
然后博主在仿照写代码时,一不留神将 @interface SignInViewController () 写成了 @interface SignInViewController : NSOject,然后 XCode 给出了错误提示,后面回查才发现自己把代码写错了,并且还并不了解 .m 文件中的这个 @interface SignInViewController () 是什么意思,所以就查了一波资料。
问题解析
查阅资料后发现,该处涉及到 Objective-C 中分类和扩展的知识点。
知识点分析
分类 Category
官方解释:
A category allows you to add methods to an existing class
—even to one for which you do not have the source.
Categories are a powerful feature that allows you to
extend the functionality of existing classes without subclassing.
Using categories, you can also distribute the implementation
of your own classes among several files.
Adding Methods to Classes
You can add methods to a class by declaring them in an interface file
under a category name and defining them in an implementation file
under the same name.
The category name indicates that the methods are additions to a class declared elsewhere,
not a new class.
You cannot, however, use a category to add additional instance variables to a class.
The methods the category adds become part of the class type.
Category methods can do anything that methods defined in the class proper can do.
At runtime, there’s no difference.
The methods the category adds to the class are inherited by all the class’s subclasses,
just like other methods.
The declaration of a category interface looks very much like a class interface declaration—
except the category name is listed within parentheses after the class name
and the superclass isn’t mentioned.
Unless its methods don’t access any instance variables of the class,
the category must import the interface file for the class it extends:
#import "ClassName.h"
@interface ClassName ( CategoryName )
// method declarations
@end
Note that a category can’t declare additional instance variables for the class;
it includes only methods.
However, all instance variables within the scope of the class
are also within the scope of the category.
That includes all instance variables declared by the class,
even ones declared @private.
There’s no limit to the number of categories that you can add to a class,
but each category name must be different,
and each should declare and define a different set of methods.
分类允许开发者为一个已有的类添加新功能,即便开发者没有该类的源码也可以如此使用。分类给予了开发者一个扩展已有类功能,而无需继承该类的强大特性,提供了不同于继承的便利。通过分类为类添加的方法就自然而然成为该类的一部分,类似于该类原来已有的方法,所有继承自该类的子类均自动拥有这些方法。
注意:
- 可以通过分类为已有类添加新功能,而不能通过分类添加新的实例变量;
- 除非在分类中实现的方法均无需访问该类中的实例变量,否则在分类中必须导入类的 interface 文件;
- 导入了类的 interface 文件后,该类中定义的所有实例变量均自动被分类所拥有,即便是 @private 定义的实例变量,分类也可以访问;
- 可以为一个类添加任意数量的分类,只需要保证每个分类名称及定义在分类中的方法不同即可。
分类的使用:
使用步骤:
第一步:创建一个在指定分类名下带有接口的新文件,在该文件中添加想要添加的新功能;
第二步:在同名 implementation 文件中实现这些功能
#import <Foundation/Foundation.h>
/** NSString 表示将要添加分类的类名称,该类必须是已存在的。
* Test 为分类名称
* testString 为新添加的方法
*/
@interface NSString (Test)
-(NSString*) testString;
@end
@implementation NSString (Test)
-(NSString*) testString
{
// 方法实现
NSString str = @"test string";
return str;
}
@end
扩展 Extension
官方解释:
Class extensions are like anonymous categories,
except that the methods they declare must be implemented
in the main @implementation block for the corresponding class.
Using the Clang/LLVM 2.0 compiler, you can also declare properties
and instance variables in a class extension.
A common use for class extensions is to redeclare property that
is publicly declared as read-only privately as readwrite:
@interface MyClass : NSObject
@property (retain, readonly) float value;
@end
// Private extension, typically hidden in the main implementation file.
@interface MyClass ()
@property (retain, readwrite) float value;
@end
Notice that (in contrast to a category) no name is given in the parentheses
in the second @interface block.
It is also generally common for a class to have a publicly declared API and
to then have additional methods declared privately for use solely by the class
or the framework within which the class resides. Class extensions allow you
to declare additional required methods for a class in locations other than
within the primary class @interface block.
大意为:扩展和匿名分类很像,不过和匿名分类还是有很大的区别,扩展中可以添加实例变量,扩展中定义的方法必须在相应类的 @implementation 代码块中实现。从 Xcode 4 之后就推荐在自定义类的 .m 文件中使用扩展,这样就能保证良好的代码封装性,避免把私有接口暴露给外面。
扩展的惯用法:
- 重新将一个在外部 interface 中声明为 readonly 的变量声明为 readwrite 类型,既便于在当前类中读写该变量,又能够避免外部修改;
- 在外部 interface 中声明公有 API,而在扩展中声明私有方法,可以避免将私有接口暴露于外部。
扩展的使用步骤:
第一步:创建 MyClass.h 文件,在 @interface MyClass 中声明可供外部调用的公有 API;
第二步:创建 MyClass.m 文件,在 @interface MyClass(){} 扩展中声明所需的实例变量和方法;
第三步:在 @implementation 代码块中实现在 MyClass.h 文件和 @interface MyClass(){} 扩展中声明的方法。
// 如下代码位于 MyClass.h 文件中
@interface MyClass : NSObject
- (float)value;
@end
// 如下代码位于 MyClass.m 文件中
@interface MyClass () { // 扩展
float value;
}
- (void)setValue:(float)newValue;
@end
// 实现
@implementation MyClass
- (float)value {
return value;
}
- (void)setValue:(float)newValue {
value = newValue;
}
@end
分类与扩展的区别
- 分类只能为已有类添加新功能,不能在分类中添加新的实例变量;而扩展不仅可以添加新功能,还能添加实例变量;
- 通过分类为已有类添加新功能时,需要在类名后面的小括号中指定分类名,同时可以通过指定多个分类名为已有类添加这多个分类中的功能;而使用扩展时,类名后的小括号中不添加任何东西;
- 扩展中声明的方法必须在相应类的 @implementation 代码块中实现,而分类则没有此要求,也就是说分类与类之间是独立的,不与特定的类相关,而扩展是与特定的类相关的,所以在扩展中声明的代码必须在相关类的 @implementation 主代码块中实现