OC語言day06-11分類練習

pragma mark 分類練習

pragma mark 概念

/**
 ARC 想適用 MRC的東西 則添加 
 -fno-objc-arc
 
 MAC 想適用 ARC的定向 則添加
  -f-objc-arc
 */

pragma mark 代碼

#import <Foundation/Foundation.h>
#pragma mark 類
#import "NSString+GetNSStringsContainsNumberCount.h" // 獲取字符串的長度

#pragma mark main函數
int countWithStr(NSString *str);
int main(int argc, const char * argv[])
{
    /*
     已知 一個字符串, 要求找到字符串所有的亞拉伯數字
     @"dasdahsdk1123daskjdhs3"
     */
    
#warning 思想
    /*
//    1. 計算器思想, 定義一個變量 保存結果 \
      2. 遍歷字符串, 取出 字符串中所有的字符
    
    // 定義一個變量 記錄阿拉伯的數字的個數
    int count = 0;

    NSString *str = @"dasdahsdk1123daskjdhs32";
    // 既然是對象 那么這個對象里面應該有方法可以使用。
#warning characterAtIndex 根據一個字符的索引,返回一個字符(unichar)[unsigned short(unsigned 最高位不作為符號位,返回的數就更大了) 可以存儲漢字]
//    unichar c =  [str characterAtIndex:0];
//    NSLog(@"%c",c);
    // 遍歷字符串
    for (int i = 0; i<str.length; i++) {
        // 在這里獲取每一個字符
        unichar c = [str characterAtIndex:i];
        NSLog(@"%c",c);
        
        // 判斷字符是不是數字
        // '0' 字符 如果寫 0 會出問題 因為字符串存儲的是ASCII碼值
        if (c >= '0' && c<= '9') {
            count ++;
        }
    }
    
    NSLog(@"count = %i",count);
     */

#warning 使用main函數的自定義方法
//    int count = countWithStr(@"112dasdashkdh3");
//    NSLog(@"count = %i",count);
    
#warning 為NSString 添加一個countWithStr方法
//    NSString *str = @"dasdahsdk1123daskjdhs32";
//    int count = [NSString countWithStr:str];
//    NSLog(@"count = %i",count);
    
#warning 其他方法獲取字符串的阿拉伯數量
    NSString *str = @"dasdahsdk1123daskjdhs32";
    int count = [str count];
    NSLog(@"count = %i",count);
    return 0;
}

/*
// 統計一個字符串里面 包含了多少個阿拉伯數組
int countWithStr(NSString *str)
{
    
    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 ++;
        }
    }
    return count;
}
*/

Person.h //人類
#import <Foundation/Foundation.h>

@interface Person : NSObject

- (void)text;
@end
Person.m
#import "Person.h"
#import "NSString+GetNSStringsContainsNumberCount.h"
@implementation Person

- (void)text
{
    NSString *str = @"1313daksjdhaskdhda1";
    int count = [NSString countWithStr:str];
    NSLog(@"count = %i",count);
}
@end

NSString+GetNSStringsContainsNumberCount.h //字符串的分類
#import <Foundation/Foundation.h>

@interface NSString (GetNSStringsContainsNumberCount)

/**
 根據字符串 來 計算字符串包含了 阿拉伯的個數
 */
+ (int)countWithStr:(NSString *)str;

- (int)count;
@end
GetNSStringsContainsNumberCount.m
#import "NSString+GetNSStringsContainsNumberCount.h"

@implementation NSString (GetNSStringsContainsNumberCount)

#warning 類方法
+ (int)countWithStr:(NSString *)str
{
    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 ++;
        }
    }
    return count;
}

#warning 對象方法
- (int)count
{
    int number= 0;
    // 這里的self 就是str
    for (int i = 0; i<self.length; ++i) {
        unichar c = [self characterAtIndex:i];
        NSLog(@"%c",c);
        if (c >= '0' && c<= '9') {
            number ++;
        }
    }
    return number;
}
@end

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容