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 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
复制代码

 

本文转自农夫山泉别墅博客园博客,原文链接:http://www.cnblogs.com/yaowen/p/7436231.html,如需转载请自行联系原作者

相关文章
|
Java iOS开发 C++
Objective-C类别(category)和扩展(Extension)的基本概念
Objective-C类别(category)和扩展(Extension)的基本概念
171 0
|
存储
OC:关于Category、load、initialize的那些事你还记得吗?
这篇文章主要分析Category的实现原理,load方法和initialize方法调用方式、调用时机、调用顺序、以及他们的区别,解释 Catgory 与 class Extension 有什么区别。
125 0
SAP SD 基础知识之行项目类别(Item Category)
SAP SD 基础知识之行项目类别(Item Category)
SAP SD 基础知识之行项目类别(Item Category)
|
供应链 计算机视觉
SAP SD 基础知识之计划行类别(Schedule Line Category)
SAP SD 基础知识之计划行类别(Schedule Line Category)
SAP SD 基础知识之计划行类别(Schedule Line Category)
Object C学习笔记14-分类(category)
  在.NET中有一个非常带劲的特性,那就是扩展方法. 扩展方法使你能够向现有类型“添加”方法(包括你自定义的类型和对象噢),而无需创建新的派生类型、重新编译或以其他方式修改原始类型。扩展方法是一种特殊的静态方法,但是可以像扩展类型上的实例方法一样进行调用。
900 0
|
JSON 数据格式