您的位置:首页 > 编程语言 > Go语言

oc60--Category 分类 练习

2017-08-26 19:51 120 查看
//  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 0;
}


//  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=0;
for (int i=0; i< str.length; i++) {
unichar c=[str characterAtIndex:i];
if (c>='0'&& c<='9') {
count++;
}
}

}

-(int)count{
int number=0;
for (int i= 0; i< self.length; ++i) {
unichar c=[self characterAtIndex:i];
if(c>='0'&& c<='0');
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐