NSString

2017年7月10日
一.添加了string保護(hù)宏 potectStringV ,兼容后臺數(shù)據(jù)返回NSNull 類型
1.實(shí)現(xiàn)

#define potectStringV(value) [HuConfigration stringValue:value]
+ (NSString*)stringValue:(id)value
{
    NSString *reslut = @"";
    if ([value isKindOfClass:[NSNumber class]]){
        reslut = [NSString stringWithFormat:@"%lld",[(NSNumber*)value longLongValue]];
    }else if([value isKindOfClass:[NSNull class]]){
        return @"";
    }
    else {
        reslut = value;
    }
    
    return reslut;
}

2.使用

NSString * title = potectStringV(data[@"title"]);

2017年5月14日
1.小寫

NSString *tmpStr = [NSString stringWithFormat:@"%@=%@",[key lowercaseString],param[key]];

2017年4月24日
1.NSString 和 NSArray之間轉(zhuǎn)化

//1.1
//systemMsgFlag參數(shù)不要空格 后臺沒做判斷
    NSArray *flagArr = @[@(HuAppServerMsgFlagTypeMyHospital),@(HuAppServerMsgFlagTypeSystem),
                         @(HuAppServerMsgFlagTypeFlower),@(HuAppServerMsgFlagTypeComment),
                         @(HuAppServerMsgFlagTypeTrain),@(HuAppServerMsgFlagTypeExam)];

    NSString *sysMsgFlag = [flagArr componentsJoinedByString:@","];
//1.2
    NSArray *strArray = [model.downLoadUrl componentsSeparatedByString:@"/"];

    NSString *name =[strArray lastObject];

2017年4月14日
一.類型轉(zhuǎn)換

    - NSString * NSStringFromClass (Class aClass);
    - Class NSClassFromString (NSString *aClassName);
    -
    - NSString * NSStringFromProtocol (Protocol *proto);
    - Protocol *NSProtocolFromString (NSString *namestr);
    -
    - NSString *NSStringFromSelector (SEL aSelector);
    - SEL NSSelectorFromString (NSString *aSelectorName);

eg:
1.string與class轉(zhuǎn)換

UIViewController *controller = (UIViewController*)NSClassFromString(specialVc);
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));

2017年3月9日
一.查找第一個符合條件的值 設(shè)定顏色
1.效果:


Paste_Image.png

2.1給所有符合條件的值 設(shè)定顏色

        NSString *sourceStr = courseModel.courseName;
        NSString *key = model.searchText ?: @"";
        //初始化NSRegularExpression
        NSRegularExpression *regularExpression = [NSRegularExpression regularExpressionWithPattern:key options:0 error:nil];
        NSMutableAttributedString *attribtStr = [[NSMutableAttributedString alloc] initWithString:sourceStr];
        //遍歷字符串,usingBlock中返回子字符串的狀態(tài),在usingBlock中處理子字符串
        [regularExpression enumerateMatchesInString:attribtStr.string options:0 range:NSMakeRange(0, attribtStr.string.length) usingBlock:^(NSTextCheckingResult * _Nullable result, NSMatchingFlags flags, BOOL * _Nonnull stop) {

            [attribtStr addAttribute:NSForegroundColorAttributeName
                                  value:[HuConfigration uiColorFromString:@"#50abf2"]
                                  range:result.range];
            _contentL.attributedText = attribtStr;
        }];

2.2單個

 NSString *sourceStr = courseModel.courseName;
        NSString *key = model.searchText ?: @"";
        NSRange range = [sourceStr rangeOfString:key];
        if (range.location != NSNotFound) {
            NSInteger index = range.location;
            NSInteger length = range.length;
            NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:sourceStr];
            [attributedStr addAttribute:NSForegroundColorAttributeName
                                  value:[HuConfigration uiColorFromString:@"#50abf2"]
                                  range:NSMakeRange(index, length)];
            _contentL.attributedText = attributedStr;
        }else{
            _contentL.text = sourceStr;
        }

2017年3月6日
一.給類擴(kuò)展定義屬性
1.實(shí)現(xiàn)

#import <Foundation/Foundation.h>
@interface NSString (HSPX)
@property (nonatomic, strong) NSString *protect;//字段保護(hù)屬性
@end

#import "NSString+Category.h"
#import <objc/runtime.h>
@implementation NSString (HSPX)

- (NSString *)protect
{
    NSLog(@"wy1");
    SEL key = @selector(protect);
    return objc_getAssociatedObject(self, key)?:@"";
}

- (void)setProtect:(NSString *)protect
{
    NSLog(@"wy2");
    SEL key = @selector(protect);
    objc_setAssociatedObject(self, key, protect, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

@end

2.使用(注意對應(yīng)項(xiàng)一定要不為nil,不然斷點(diǎn)進(jìn)不去,所以上面的本意默認(rèn)添加nil保護(hù)其實(shí)不起作用)

        NSString *testID = _testExerciseId.protect; //不會調(diào)用 因?yàn)開testExerciseId為nil
        NSString *test2 = @"hello";
        NSString *tmp = test2.protect;

如果您發(fā)現(xiàn)本文對你有所幫助,如果您認(rèn)為其他人也可能受益,請把它分享出去。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容