categoary分类
// #import @interface Person : NSObject { } @property (nonatomic,assign) int age; @property (nonatomic,strong) NSString * name; //人类的基本行为 //吃喝跑跳哭笑 - (void)eat; - (void)drink; - (void)run; - (void)jump; - (void)cry; - (void)smile; //恐怖分子 @end // #import "Person.h" @implementation Person - (void)eat { } - (void)drink { } - (void)run { } - (void)jump { } - (void)cry { } - (void)smile { } @end // #import "Person.h" @interface Person (BlackPerson) //不能在分类中生成员变量 //{ // //} //黑人技能 - (void)superRun; - (void)superJump; - (void)eat; @end // #import "Person+BlackPerson.h" @implementation Person (BlackPerson) //黑人技能 - (void)superRun { } - (void)superJump { // } // 如果分类中定义实现了与原类中相同的方法,那么原类中的方法相当于被覆盖掉了 // 在main函数中引入多个类扩展,而每个类扩展又都覆盖了原来的方法,那么在main函数中调用被覆盖的方法到底是 // 哪个方法这取决于文件的编译顺序,这就出现了不确定因素所有 // 在实际的开发中,最好不要出现方法覆盖 - (void)eat { } @end // #import "Person.h" @interface Person (Kongbufenzi) - (void)selfBoom; - (void)bigBoom; - (void)eat; @end // #import "Person+Kongbufenzi.h" @implementation Person (Kongbufenzi) - (void)selfBoom { } - (void)bigBoom { } - (void)eat { } @end // #import #import "Person.h" #import "Person+BlackPerson.h" #import "Person+Kongbufenzi.h" int main(int argc, const char * argv[]) { } |
分类应用<<span style="font-family:宋体;mso-ascii-font-family:"Times New Roman";mso-fareast-font-family: 宋体;mso-fareast-theme-font:minor-fareast;mso-hansi-font-family:"Times New Roman"">多用于扩展系统提供的类>
// #import #import "NSString+NSStringExtend.h" int main(int argc, const char * argv[]) { // // // // // // // // } // #import @interface NSString (NSStringExtend) + (int)numberCountOfString:(NSString *)str; - (int)numberOfCount; @end // #import "NSString+NSStringExtend.h" @implementation NSString (NSStringExtend) //ab1c890 + (int)numberCountOfString:(NSString *)str { } - (int)numberOfCount { } @end |
block数据类型
// #import typedef int (^calculateBlock)(int a,int b); @interface Calculate : NSObject - (int)calculateWithNumber1:(int)number1 andNumber2:(int)number2 andCalculate:(calculateBlock)calculate; @end // #import "Calculate.h" @implementation Calculate - (int)calculateWithNumber1:(int)number1 andNumber2:(int)number2 andCalculate:(calculateBlock)calculate { } @end // // 本文件旨在说明block: // 可以把它理解成一个匿名函数,但是这种匿名函数是一个block类型的,它可以作为参数传递 #import #import "Calculate.h" //数据类型的作用 //1.作为参数传递 //2.作为函数的返回值 //3.声明成变量 void test() { } int sum(int a, int b) { } int main(int argc, const char * argv[]) { // OC中新增加下面几个类型 // 其内容可以是加减陈处或其它运算,这样在需求发生变化时,更容易维护修改> } 补充点:指向函数的指针
|
protocol(协议)
|
SEL数据类型
// #import @interface Person : NSObject - (void)eat; - (void)call:(NSString *)number; - (NSString *)findName; @end // #import "Person.h" @implementation Person - (void)eat { } - (void)call:(NSString *)number { } - (NSString *)findName { } @end // #import #import "Person.h" int main(int argc, const char * argv[]) { // // } |