• iOS


    分类(Category)
     
    分类能够做到的事情主要是:即使在你不知道一个类的源码情况下,向这个类添加扩展的方法。
     
    此外,分类能够保证你的实现类和其他的文件区分开。
     
    1 #import “UIViewController.h”
    2 @interface UIViewController(CustomView)
    3 -(void)extMethod;
    4 @end
     使用分类为类添加方法(Add Methods to Classes)
     
    通过在interface中声明一个额外的方法并且在implementation 中定义相同名字的方法即可。分类的名字(也就是括号括起来的CustomView)表示的是:对于声明于其他地方的这个类(UIViewController),在此处添加的方法是额外的,而不是表示这是一个新的类。你不可以通过分类为一个类添加额外的成员变量。
     
    在implementation中,引入头文件的时候主要引用的方式是:
     
    1 #import “UIViewController+CustomView.h”
    2 @implementation UIViewController(CustomView)
    3 -(void)extMethod;
    4 @end
     另外,虽然Category不能够为类添加新的成员变量,但是Category包含类的所有成员变量,即使是@private的。Category可以重新定义新方法,也可以override继承过来的方法。
     
    类扩展(Class Extensions)
     
    类扩展就像匿名(也就是没有那个括号里面的名字CustomView)的分类一样,除了一样不同的是,类扩展声明必须在@implementation在实现。
     
    先看一段代码:
    1 @interface MyObject:NSObject
     2 {
     3 NSNumber* number;
     4 }
     5 -(NSNumber*)getNum;
     6 @end
     7  
     8 @interface MyObject(Setter)
     9 -(void)setNum:(NSNumber*)num;
    10 @end
    11  
    12 @implementation MyObject
    13 -(NSNumber*)getNum
    14 {
    15 return number;
    16 }
     看上面这段代码,有没有问题?编译器编译的时候,这段代码是可以编译通过,但当运行时,就会报错。为什么?
     
    因为没有实现Category中的setNum方法。而用类扩展去实现,请看:
    1 @interface MyObject:NSObject
     2 {
     3 NSNumber* number;
     4 }
     5 -(NSNumber*)getNum;
     6 @end
     7  
     8 @interface MyObject() //注意这里的括号里面是没有名字的
     9 -(void)setNum:(NSNumber*)num;
    10 @end
    11  
    12 @implementation MyObject
    13 -(NSNumber*)getNum
    14 {
    15 return number;
    16 }
    17  
    18 -(void)setNum:(NSNumber*)num
    19 {
    20 number = num;
    21 }
    22 @end

     setNum是必须要实现,不然编译器会提出警告。

     
    从上面看出,分类和类扩展的相似之处是:都可以为类添加一个额外的方法;
     
    不同之处在于:要添加额外方法,分类必须在第一个@interface中声明方法,并且在@implementation中提供实现,不然运行时出错。而类扩展,你添加的方法是一个required API,如果不去实现,编译器会警告,而且这个方法的声明可以不在第一个@interface中去声明。
  • 相关阅读:
    买房的贷款时间是否是越长越好?https://www.zhihu.com/question/20842791
    asp.net cookie and session
    leelazero and google colab
    download file by python in google colab
    physical processor, core, logical processor
    通过powershell操作eventlog
    openxml in sql server
    get the page name from url
    How to Execute Page_Load() in Page's Base Class?
    Difference between HttpContext.Request and Request
  • 原文地址:https://www.cnblogs.com/baitongtong/p/5982043.html
Copyright © 2020-2023  润新知