我不知道的cateGory

什么是cateGory?

  • cateGory是一种类别
  • 这是一种比继承更简洁的方法来对类进行扩展,不需创建子类就能为现有的类提供方法,还可以提供方法的重写 。

[Category相关链接] (http://cocoacats.com/)


  • 类别的优缺点

  • 缺点

  • (1)无法向类别中(interface)添加新的实体变量
* (2)名称冲突 (类别方法与原始方法冲突时,类别方法优先,将完全取代初始方法,而无法在使用初始方法)

  • 优点

  • (1)可以将类的实现分散到不同的文件当中

接下来我列举一下我们项目中用到的cateGory 。

  • 加载网络图片

#import "UIImageView+AFNetworking.h"(使用cateGory加载图片)
[cell.image setImageWithURL:[NSURL URLWithString:dic[@"logoPath"]]];

 [cell.image setImageWithURL:[NSURL URLWithString:dic[@"logoPath"]]];```

* 校验是否为有效的手机号 ``#import "NSString+MALNSString.h"
``

(![_TextFieldThree.text isMobilePhoneNumber])


* 设置导航栏背景图片 ,和Title ``#import "UIViewController+MALNavigation.h"
``

[UIViewController setNavigationBarBgImage:self.navigationController andBgImage:[UIImage imageNamed:@"图片名字"]];
[self setCenterItemWithTitle:@""];


* 设置btn的点事件``#import "UIViewController+MALNavigation.h"
``

[barItem.contentBarItem addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];


* 创建Category``@interface UIImageView_Bg : UIImageView
``
把_删除在Bg处用()括住 把继承删除
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容