Snip20170927_76.png
類擴展的好處:封裝私有屬性和方法句葵。
分類練習(xí):
已知一個字符串, 要求找出字符串中所有的阿拉伯?dāng)?shù)字
@"a123jj46kfd5jlwf7ld";
1.計數(shù)器思想, 定義一個變量保存結(jié)果
2.遍歷字符串, 取出字符串中所有的字符
NSString+ZCL.h
#import <Foundation/Foundation.h>
@interface NSString (ZCL)
// + (int)countWithStr:(NSString *)str;
- (int)count;
@end
NSString+ZCL.m
#import "NSString+ZCL.h"
@implementation NSString (ZCL)
//+(int)countWithStr:(NSString *)str{
// int count=0;
// for (int i=0; i< str.length; i++) {
// unichar c=[str characterAtIndex:i];
// if (c>='0'&& c<='9') {
// count++;
// }
// }
// return count;
//}
-(int)count{
int number=0;
for (int i= 0; i< self.length; ++i) {
unichar c=[self characterAtIndex:i];
if(c>='0'&& c<='9'){
number ++;
}
}
return number;
}
@end