• OC学习笔记 xcode6 category 分类创建和使用


    xcode6 分类 catepory 位置

    使用方法:对一个字符串里面阿拉伯数字计算个数

    1 #import <Foundation/Foundation.h>
    2 
    3 @interface NSString (Number)
    4 + (int)numberCountOfString :(NSString *)str;
    5 - (int)numString;
    6 @end
     1 #import "NSString+Number.h"
     2 
     3 @implementation NSString (Number)
     4 
     5 + (int)numberCountOfString:(NSString *)str
     6 {
     7     return [str numString];
     8 }
     9 
    10 - (int)numString
    11 {
    12     int count = 0;
    13     for (int i = 0; i<self.length; i++) {
    14        unichar c = [self characterAtIndex:i];
    15         if(c >'0' && c< '9')
    16         {
    17             count ++;
    18         }
    19     }
    20     return count;
    21 }
    22 @end
     1 //
     2 //  main.m
     3 //  分类的应用
     4 //定义类方法 给NSString 扩展一个计算某个字符串中阿拉伯数字个数
     5 //  Created by zjj on 15/4/24.
     6 //  Copyright (c) 2015年 zjj. All rights reserved.
     7 //
     8 
     9 #import <Foundation/Foundation.h>
    10 #import "NSString+Number.h"
    11 int main() {
    12     int count = [NSString numberCountOfString:@"9hf8g7d7sk65j4h3g863"];
    13     NSLog(@"%d",count);// 第一种方法类方法输出
    14 NSLog(@"%d",[@"sadsad2r23f6s6d663" numString]) ; //第二种对象方法输出 推荐使用
    15     return 0;
    16 }
  • 相关阅读:
    C++中的static关键字的总结
    2017上海C++面试
    Vim 跳到上次光标位置
    Windows XP Professional产品序列号
    Centos7 安装sz,rz命令
    Xshell里连接VirtualBox里的Centos7
    什么是位、字节、字、KB、MB
    Centos7 tmux1.6 安装
    Centos7 在 Xshell里 vim的配置
    对JDBC的轻量级封装,Hibernate框架
  • 原文地址:https://www.cnblogs.com/zhangdashao/p/4452964.html
Copyright © 2020-2023  润新知