101,使用Category给API里面的类添加新方法
来源:互联网 发布:网络交友论坛 编辑:程序博客网 时间:2024/05/17 06:28
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 = [selfcharacterAtIndex:i];
if (c >= '0' && c <='9') {
count++;
}
}
return count;
}
@end
#import <Foundation/Foundation.h>
#import "NSString+JS.h"
int main(int argc,const char * argv[]) {
/*
问题:计算字符串中含有多少个数字?
知识点:为NSString添加计算数字的方法
*/
NSString *str =@"s123asf675ds3s5";
int count = [NSStringcountWithStr: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
- 101,使用Category给API里面的类添加新方法
- Objective-C category 为现有的类添加新方法的方式。
- iOS 运行时runtime应用之二--在category中使用运行时API给类添加属性
- [Objective-C] 使用Category给类添加private方法
- Category 类扩展,就是给原有类的添加方法。
- category 给IOS原生类添加属性
- 用Category给类中添加属性
- 给Category添加属性
- 修改 toString 的原意 和给 Object 添加新方法
- Myeclipse2015 Jrebel插件的安装、配置、使用(新建.java文件、给类添加新方法、新属性、修改代码等,不用重启tomcat即可更新)
- iOS 给category添加属性的小技巧
- Category能否给已有类添加新的属性
- javascript给builtin对象添加新方法
- iOS开发:给项目工程里面添加Podfile配置文件,以及CocoaPods的简单使用
- ASP.NET 2.0中给DropDownList服务器控件添加项的新方法
- ASP.NET 2.0中给DropDownList服务器控件添加项的新方法
- ASP.NET 2.0中给DropDownList服务器控件添加项的新方法
- ASP.NET 2.0中给DropDownList服务器控件添加项的新方法
- kali攻防第11章 攻击实例讲解-ms10_046快捷方式图标漏洞
- PHP接口与继承的简单实例
- OSCtxSW()函数在80x86上移植的理解
- C语言中的柔性数组
- 绘图 - 9
- 101,使用Category给API里面的类添加新方法
- Android ListView下拉/上拉刷新:设计原理与实现
- OpenSSL-0.9.8g 安装与配置指南
- 论文次序哪个为准
- 如何把前台传的两个date类型在js中转换成天
- 黑马程序员 - OC语言 - NSString前后缀检查及搜索
- SD卡分区创建详细教程
- spring mvc 框架项目tomcat 移植到WebSphere
- ios搭一个简易计算器(利用masonry布局)