iOS 类的分类 Category
iOS 中的分类,特别的好用。
什么是分类
Category 模式用于向已经存在的类添加方法从而达到扩展已有类的目的,在很多情形下 Category 也是比创建子类更优的选择。新添加的方法同样也会被被扩展的类的所有子类自动继承。当知道已有类中某个方法有BUG,但是这个类是以库的形式存在的,我们无法直接修改源代码的时候,Category 也可以用于替代这个已有类中某个方法的实体,从而达到修复 BUG 的目的。然而却没有什么便捷的途径可以去调用已有类中原有的那个被替换掉方法实体了。需要注意的是,当准备有 Category 来替换某一个方法的时候,一定要保证实现原来方法的所有功能,否则这种替代就是没有意义而且会引起新的 BUG 。和子类不同的是,Category 不能用于向被扩展类添加实例变量。Category 通常作为一种组织框架代码的工具来使用。
总结一句就是:扩展方法,但是不能扩展属性
- 如果需要添加一个新的变量,则需添加子类。
- 如果只是添加一个新的方法,用 Category 是比较好的选择。
用法
//.h文件
@interface ClassName (CategoryName)
-methodName1
-methodName2
@end
//.m文件
@implementation ClassName (CategoryName)
-methodName1
-methodName2
@end
示例代码1
//苹果官方的 UIView 不支持直接修改宽高等等各种属性,这个时候我们可以写一个分类
.h文件
@interface UIView (MJ)
@property (assign, nonatomic) CGFloat x;
@property (assign, nonatomic) CGFloat y;
@property (assign, nonatomic) CGFloat width;
@property (assign, nonatomic) CGFloat height;
@property (assign, nonatomic) CGSize size;
@property (assign, nonatomic) CGPoint origin;
@end
.m文件
@implementation UIView (MJ)
- (void)setX:(CGFloat)x
{
CGRect frame = self.frame;
frame.origin.x = x;
self.frame = frame;
}
- (CGFloat)x
{
return self.frame.origin.x;
}
- (void)setY:(CGFloat)y
{
CGRect frame = self.frame;
frame.origin.y = y;
self.frame = frame;
}
- (CGFloat)y
{
return self.frame.origin.y;
}
- (void)setWidth:(CGFloat)width
{
CGRect frame = self.frame;
frame.size.width = width;
self.frame = frame;
}
- (CGFloat)width
{
return self.frame.size.width;
}
- (void)setHeight:(CGFloat)height
{
CGRect frame = self.frame;
frame.size.height = height;
self.frame = frame;
}
- (CGFloat)height
{
return self.frame.size.height;
}
- (void)setSize:(CGSize)size
{
CGRect frame = self.frame;
frame.size = size;
self.frame = frame;
}
- (CGSize)size
{
return self.frame.size;
}
- (void)setOrigin:(CGPoint)origin
{
CGRect frame = self.frame;
frame.origin = origin;
self.frame = frame;
}
- (CGPoint)origin
{
return self.frame.origin;
}
@end
示例代码 2
MBProgressHUD 的一个分类,原作者为李明杰
MBProgressHUD 实在太难用,这个分类可以简化很多操作
废话不多说,上代码
//
// MBProgressHUD+LJ.h
// lntuApp
//
// Created by JieLee on 15-01-05.
// Copyright (c) 2015年 PUPBOSS. All rights reserved.
//
#import "MBProgressHUD.h"
@interface MBProgressHUD (LJ)
+ (void)showSuccess:(NSString *)success toView:(UIView *)view;
+ (void)showError:(NSString *)error toView:(UIView *)view;
+ (MBProgressHUD *)showMessage:(NSString *)message toView:(UIView *)view;
+ (void)showSuccess:(NSString *)success;
+ (void)showError:(NSString *)error;
+ (MBProgressHUD *)showMessage:(NSString *)message;
+ (void)hideHUDForView:(UIView *)view;
+ (void)hideHUD;
@end
//
// MBProgressHUD+LJ.h
// lntuApp
//
// Created by JieLee on 15-01-05.
// Copyright (c) 2015年 PUPBOSS. All rights reserved.
//
#import "MBProgressHUD+LJ.h"
@implementation MBProgressHUD (LJ)
#pragma mark 显示信息
+ (void)show:(NSString *)text icon:(NSString *)icon view:(UIView *)view
{
if (view == nil) view = [[UIApplication sharedApplication].windows lastObject];
// 快速显示一个提示信息
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
hud.labelText = text;
// 设置图片
hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"MBProgressHUD.bundle/%@", icon]]];
// 再设置模式
hud.mode = MBProgressHUDModeCustomView;
// 隐藏时候从父控件中移除
hud.removeFromSuperViewOnHide = YES;
// 0.9秒之后再消失
[hud hide:YES afterDelay:0.9];
}
#pragma mark 显示错误信息
+ (void)showError:(NSString *)error toView:(UIView *)view {
[self show:error icon:@"error.png" view:view];
}
+ (void)showSuccess:(NSString *)success toView:(UIView *)view {
[self show:success icon:@"success.png" view:view];
}
#pragma mark 显示一些信息
+ (MBProgressHUD *)showMessage:(NSString *)message toView:(UIView *)view {
if (view == nil) view = [[UIApplication sharedApplication].windows lastObject];
// 快速显示一个提示信息
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
hud.labelText = message;
// 隐藏时候从父控件中移除
hud.removeFromSuperViewOnHide = YES;
// YES代表需要蒙版效果
hud.dimBackground = YES;
return hud;
}
+ (void)showSuccess:(NSString *)success
{
[self showSuccess:success toView:nil];
}
+ (void)showError:(NSString *)error
{
[self showError:error toView:nil];
}
+ (MBProgressHUD *)showMessage:(NSString *)message
{
return [self showMessage:message toView:nil];
}
+ (void)hideHUDForView:(UIView *)view
{
[self hideHUDForView:view animated:YES];
}
+ (void)hideHUD
{
UIView *view = [[UIApplication sharedApplication].windows lastObject];
[self hideHUDForView:view];
}
@end