101,使用Category给API里面的类添加新方法
2015-12-18 11:23
429 查看
NSString+JS.h:
#import <Foundation/Foundation.h>
@interface NSString (JS)
+(int)countWithStr:(NSString *)str;
-(int)count;
@end
NSString+JS.m:
#import "NSString+JS.h"
@implementation NSString (JS)
+(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{
//self:谁调用该函数就是谁
int count = 0;
for (int i =
0; i < self.length; i++) {
unichar c = [self
characterAtIndex:i];
if (c >= '0' && c <=
'9') {
count++;
}
}
return count;
}
@end
main.m:
#import <Foundation/Foundation.h>
#import "NSString+JS.h"
int main(int argc,
const char * argv[]) {
/*
问题:计算字符串中含有多少个数字?
知识点:为NSString添加计算数字的方法
*/
NSString *str =
@"s123asf675ds3s5";
int count = [NSString
countWithStr:str];
int count1 = [str
count];
NSLog(@"count = %i,count1 = %i",count,count1);
return 0;
}
//2015-12-18 11:18:29.492 8,使用Category为系统类添加方法[982:50190] count = 8,count1 = 8
//Program ended with exit code: 0
相关文章推荐
- Django - “no module named django.core.management”
- Alpine里的go应用,你猜他能有多小?
- ubuntu 12.04 lts安装golang并设置vim语法高亮
- 启发式算法(heuristic algorithm)
- Django-1.9
- 接入google play service 的debug模式测试时候弹出选择谷歌账户后回调失败的原因【三个小坑真TM的经典】
- Django 设置template的全局变量
- ZZUOJ-10434: good string
- Django adminsite
- Codeforces Round #313 (Div. 2) 560C Gerald's Hexagon(脑洞)
- ZJPC-CTF_2015.11~12_writeup_by_GoldsNow
- 在阿里云ubuntu 14.04 上部署Gogs
- 在Go语言中记录log:seelog包
- 看godoc的命令
- 99,Category基本概念与注意点
- Genetic Algorithm遗传算法学习
- Windows Phone版使用移动广告平台集成Google Mobile Ads SDK教程
- Google 的开源技术protobuf 简介与例子
- cordova 工程中 引用google分析的功能。
- google protobuf学习笔记二:使用和原理