通过Category 添加的方法会成为元类的的一部分 达到扩展类的功能
#import "NSArray+EasyLog.h"
@implementation NSArray (EasyLog)
-(NSString *)descriptionWithLocale:(id)locale{ //self.count数组个数 字符串无符号长整型unsigned long
NSMutableString *str = [NSMutableString stringWithFormat:@"%lu(\n",(unsigned long)self.count];
for (id obj in self) {
[str appendFormat:@"\t %@,\n",obj];// \t 前进四格
}
[str appendFormat:@")"];
return str;
}
-(void)PrintArrayCount{
NSLog(@"当前数组元素个数:%lu",self.count);
}
@end
NSArray *myLove = [NSArray arrayWithObjects:@"爸爸",@"妈妈",@"弟弟",@"妹妹",@"My刘先生", nil];
// NSLog(@"%@",myLove);
// [myLove PrintArrayCount];
//extension(扩展 延展) Category(类目) 区别
/* 1、作用:
类目是为没有源代码的类扩展的方法
延展的作用是管理类的私有方法
2、格式上 :
类目需要定义一个新的.h .m
延展可以直接写到现有类的.m文件中
*/extension(扩展 延展)在自己类中扩展
#import "Myself.h"
@interface Myself (Myself_Extention)
-(void)myWright;
-(void)myGender;
-(void)myPhone;
-(void)myBankCardPassWord;
@end
@implementation Myself
-(void)myWright{
NSLog(@"myWright:98");
}
-(void)myGender{
NSLog(@"myGender:女");
}
-(void)myPhone{
NSLog(@"myPhoneNumber:123456");
}
-(void)myBankCardPassWord{
NSLog(@"myBankCardPassWord:110");
}
@end
protocol 协议 代理
n/Foundation.h>
//MakeBoyFriend 协议名字
@protocol MakeBoyFriend <NSObject>
//协议中默认是必须实现的
-(void)makeMoney;//会挣钱
-(void)doHouseWork;
-(void)kind;
-(void)tall;
-(void)someTimeAngry;//偶尔不生气
-(void)cook;
-(void)youmo;
-(void)faceBeautiful;//漂亮
@optional //可选择的 在协议中默认方法必须实现 如果加上@optional 下边的方法可选择实现,实现也可以 不实现也没问题
-(void)washCloth;//会洗衣
-(void)angry; //生气
@end
#import "LiuXianShen.h"
#import "MakeBoyFriend.h"
@implementation LiuXianShen
-(void)makeMoney{//会挣钱
NSLog(@"挣钱");
}
-(void)doHouseWork{
NSLog(@"做家务");
}
-(void)kind{
NSLog(@"善良");
}
-(void)tall{
NSLog(@"178");
}
-(void)someTimeAngry{//偶尔不生气
NSLog(@"不生气");
}
-(void)cook{
NSLog(@"会做美食");
}
-(void)youmo{
NSLog(@"幽默风趣");
}
-(void)faceBeautiful{//漂亮
NSLog(@"帅气,真男人");
}
-(void)washCloth{
NSLog(@"亲爱的,拿来给你洗");
}//会洗衣
@end
#import "Me.h"
@implementation Me
-(void)hungry{//饿了
[self.BoyFriend cook];
}
-(void)clothDirty{//衣服脏了
[self.BoyFriend washCloth];
}
-(void)makeMoney{
[self.BoyFriend makeMoney];
}//看着愉悦
-(void)lookHappy{
[self.BoyFriend faceBeautiful];
}
@end
LiuXianShen *boy = [[LiuXianShen alloc]init];
Me *zhaoJiaHui = [[Me alloc]init];
zhaoJiaHui.BoyFriend = boy;
//设置代理 LiuXianShen 遵循MakeBoyFriend 协议 把boy赋值给右边的我 表明boy已成了你的代理 也就是说boy遵循MakeBoyFriend的协议 这个boy要为我服务
//协议MakeBoyFriend 遵循协议LiuXianShen 遵循协议的要为我服务
[zhaoJiaHui hungry];
[zhaoJiaHui clothDirty];