NSString

2017年7月10日
一.添加了string保护宏 potectStringV ,兼容后台数据返回NSNull 类型
1.实现

#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之间转化

//1.1
//systemMsgFlag参数不要空格 后台没做判断
    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日
一.类型转换

    - 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转换

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

2017年3月9日
一.查找第一个符合条件的值 设定颜色
1.效果:


Paste_Image.png

2.1给所有符合条件的值 设定颜色

        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中返回子字符串的状态,在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日
一.给类扩展定义属性
1.实现

#import <Foundation/Foundation.h>
@interface NSString (HSPX)
@property (nonatomic, strong) NSString *protect;//字段保护属性
@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.使用(注意对应项一定要不为nil,不然断点进不去,所以上面的本意默认添加nil保护其实不起作用)

        NSString *testID = _testExerciseId.protect; //不会调用 因为_testExerciseId为nil
        NSString *test2 = @"hello";
        NSString *tmp = test2.protect;

如果您发现本文对你有所帮助,如果您认为其他人也可能受益,请把它分享出去。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容