分类(类别)的应用 Category
/*
分类的作用:在不改变原来类内容的基础上,可以为类增加一些方法
使用注意:
1.分类只能增加方法,不能增加成员变量
2.分类方法实现中可以访问原来类中声明的成员变量
3.分类可以重新实现原来类中的方法,但是会覆盖掉原来的方法,会导致原来的方法没法再使用
4.方法调用的优先级:分类(最后参与编译的分类优先) --> 原来类 --> 父类
*/
#import "Person.h"
#import "Person+One.h"
#import "Person+Two.h"
int main()
{
Person *p = [[Person alloc] init];
//p.age = 10;
// 优先去分类中查找,然后再去原来类中找,最后再去父类中找
[p test];
// [p study];
return 0;
}
主类
@interface Person : NSObject
{
int _age;
}
@property int age;
- (void)test;
@end
1
2
3
4
5
6
7
@implementation Person
- (void)test
{
NSLog(@"Person-test");
}
@end
分类一
#import "Person.h"
@interface Person (One)
- (void)test2;
@end
1
2
3
4
5
6
#import "Person+One.h"
@implementation Person (One)
- (void)test2
{
NSLog(@"-----test2");
}
- (void)test
{
NSLog(@"Person (One)-test");
}
@end
分类二
<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#import <span class="hljs-title" style="box-sizing: border-box;">"Person.h"</span></span>
<span class="hljs-class" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">@interface</span> <span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">Person</span> (<span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">Two</span>)</span>
- (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span>)study;
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">@end</span>
</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li></ul>
<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#import <span class="hljs-title" style="box-sizing: border-box;">"Person+Two.h"</span></span>
<span class="hljs-class" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">@implementation</span> <span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">Person</span> (<span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">Two</span>)</span>
- (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span>)study
{
<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"学习-----%d"</span>, _age);
}
- (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span>)test
{
<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"Person (Two)-test"</span>);
}
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">@end</span></code>
分类的应用
/*
给NSString增加一个类方法:计算某个字符串中阿拉伯数字的个数
给NSString增加一个对象方法:计算当前字符串中阿拉伯数字的个数
*/
#import <Foundation/Foundation.h>
@interface NSString (Number)
+ (int)numberCountOfString:(NSString *)str;
- (int)numberCount;
@end
@implementation NSString (Number)
// @"abc434ab43"
+ (int)numberCountOfString:(NSString *)str
{
//1.定义变量计算数字的个数
int count = 0;
for (int i = 0; i<str.length; i++)
{
unichar c = [str characterAtIndex:i];
if ( c>='0' && c<='9')
{
count++;
}
}
return count;
return [str numberCount];
}
- (int)numberCount
{
int count = 0;
for (int i = 0; i<self.length; i++)
{
// 取出i这个位置对应的字符
unichar c = [self characterAtIndex:i];
// 如果这个字符是阿拉伯数字
if ( c>='0' && c<='9' )
{
count++;
}
}
return count;
}
@end
#import <Foundation/Foundation.h>
#import "NSString+Number.h"
int main()
// 类库:很多类的集合
{
// int count = [NSString numberCountOfString:@"54d43a43s43dasd"];
int count = [@"9fdsfds543543" numberCount];
NSLog(@"%d", count);
return 0;
}