oc60--Category 分类 练习
// main.m
// Category练习 #import <Foundation/Foundation.h>
#import "NSString+NJ.h" //看不到NSString的.h文件。 /*
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 main(int argc, const char * argv[]) {
/*
已知一个字符串, 要求找出字符串中所有的阿拉伯数字
@"a123jj46kfd5jlwf7ld"; 1.计数器思想, 定义一个变量保存结果
2.遍历字符串, 取出字符串中所有的字符
*/ NSString *str = @"a1jj46kf1d5jlwf7l9d8";
/*
// unichar c = [str characterAtIndex:1];
// NSLog(@"%c", c);
int count = 0;
for (int i = 0; i < str.length; ++i) {
unichar c = [str characterAtIndex:i];
// NSLog(@"%c", c);
if (c >= '0' && c <= '9') {
count++;
}
}
*/ int count2 = countWithStr(str);
int count1 = [NSString countWithStr:str];
int count = [str count];
NSLog(@"count = %i", count);
return ;
}
// NSString+NJ.h #import <Foundation/Foundation.h> @interface NSString (NJ) + (int)countWithStr:(NSString *)str; - (int)count;
@end
// NSString+NJ.m #import "NSString+NJ.h" @implementation NSString (NJ) -(int)countWithStr:(NSString *)str{
int count=;
for (int i=; i< str.length; i++) {
unichar c=[str characterAtIndex:i];
if (c>=''&& c<='') {
count++;
}
} } -(int)count{
int number=;
for (int i= ; i< self.length; ++i) {
unichar c=[self characterAtIndex:i];
if(c>=''&& c<='');
number ++; } }
@end
// Person.h #import <Foundation/Foundation.h> @interface Person : NSObject - (void)test;
@end
// Person.m #import "Person.h"
#import "NSString+NJ.h" @implementation Person -(void)test{
NSString *str=@"fds64jkl43fjdslkf";
int count =[NSString countWithStr:str];
NSLog(@" count= %i",count);
} @end
oc60--Category 分类 练习的更多相关文章
- category分类
/* 使用继承关系来扩充一个类,有一个弊病,高耦合性 category(分类,类别) 能够帮我们扩充一个类的功能 */ - (void)superJump { // [self eat]; [s ...
- Objective-C中的Category(分类)
Objective-C中的Category(分类) 1 Category概念:动态的为已经存在的类加入新的行为(方法) 2 Category(分类)创建的方法 (1)通过Xcode生成分类 (2)能够 ...
- 使用django开发博客过程记录4——Category分类视图
在写点击博客的所属分类,显示所有该分类的文章时真是让我想了好一会,为什么呢?因为我使用的是cbv模式开发的而不是简单的视图处理逻辑的,所以,有些操作会被包装好了,你并不知道它的细节,那么我们今天要实现 ...
- Objective-C( Category 分类,非正式协议,分类延展)
分类: 主要是给原来类增加一些方法;不可以增加成员变量 Person (Person+eat) 非正式协议: 非正式协议就是类别,即凡是NSObject或其子类Foundation框架中的类增加的类别 ...
- iOS - OC Category 分类
1.Category 1)分类/类别(category): 允许以模块的方式向现有类定义添加新的方法(默认不能添加实例变量).用以扩展自己或他人以前实现的类,使它适合自己的需要. 分类的名称括在类名之 ...
- [Objective-c 基础 - 2.8] category分类/类别/类目
A.给某个类扩充方法(不改变原来的类) 例如,给类Person加上名为Simon的category,加上一个-study方法 使用()注明 Person+Simon.h @interface Pers ...
- Category 分类
1.Category 1)分类/类别(category): 允许以模块的方式向现有类定义添加新的方法(默认不能添加实例变量).用以扩展自己或他人以前实现的类,使它适合自己的需要. 分类的名称括在类名之 ...
- [OC笔记] Category分类之见解
用过别的语言做过开发的同学都知道,如果你想扩充一个类,就应该去继承这个类.但是OC里面有更好的方法,那就是分类. 那什么是分类呢?就是在不改变原先类,我们可以在其中添加咱们自定义的方法,这样和同事合作 ...
- OC category(分类)
// ()代表着是一个分类 // ()中的Test代表着分类的名称 @interface Student (Test) // 分类只能扩展方法,不能增加成员变量 - (void)test2; @end
- Xcode7中,如何新建category分类
易忘,所以留存: 1, 2, 3, 结果如下: 补充: http://tech.meituan.com/DiveIntoCategory.html
随机推荐
- dubbo之令牌验证
防止消费者绕过注册中心访问提供者 在注册中心控制权限,以决定要不要下发令牌给消费者 注册中心可灵活改变授权方式,而不需修改或升级提供者 可以全局设置开启令牌验证 <!--随机token令牌,使用 ...
- RHEL7配置中文输入法-智能拼音
RHEL7配置中文输入法-智能拼音 RHEL7.x(CentOS7.x)系统相对之前的6.x系统变化较大,虽然安装时选择了中文环境,但是进入系统后,在控制台及编辑器中仍无法切换输入法进行中文输入. 原 ...
- 【sqli-labs】 less52 GET -Blind based -Order By Clause -numeric -Stacked injection(GET型基于盲注的整型Order By从句堆叠注入)
出错被关闭了 http://192.168.136.128/sqli-labs-master/Less-52/?sort=1' http://192.168.136.128/sqli-labs-mas ...
- jenkins执行python脚本
参考: https://blog.csdn.net/qq_39247153/article/details/81003244 https://blog.csdn.net/huashao0602/art ...
- Mirror用法
switch (quadrantType) { case QuadrantType.one: db.setlayerCenter(); ids.Add(db.AddToModelSpace(arc)) ...
- CAD读取属性块
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...
- 有赞 MySQL 自动化运维之路 — ZanDB
转自:https://tech.youzan.com/youzan-mysql-auto-ops-road/ 一.前言 在互联网时代,业务规模常常出现爆发式的增长.快速的实例交付,数据库优化以及备份管 ...
- 【解题报告】 Leapin' Lizards HDU 2732 网络流
[解题报告] Leapin' Lizards HDU 2732 网络流 题外话 在正式讲这个题目之前我想先说几件事 1. 如果大家要做网络流的题目,我在网上看到一个家伙,他那里列出了一堆网络流的题目, ...
- exgcd扩展欧几里得求解的个数
知识储备 扩展欧几里得定理 欧几里得定理 (未掌握的话请移步[扩展欧几里得]) 正题 设存在ax+by=gcd(a,b),求x,y.我们已经知道了用扩欧求解的方法是递归,终止条件是x==1,y==0: ...
- 观察者模式之Golang实现
观察者模式的具体概念原理,参见https://baike.baidu.com/item/%E8%A7%82%E5%AF%9F%E8%80%85%E6%A8%A1%E5%BC%8F/5881786?fr ...