2017年7月10日
一.添加了string保護(hù)宏 potectStringV 休溶,兼容后臺(tái)數(shù)據(jù)返回NSNull 類(lèi)型
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.小寫(xiě)
NSString *tmpStr = [NSString stringWithFormat:@"%@=%@",[key lowercaseString],param[key]];
2017年4月24日
1.NSString 和 NSArray之間轉(zhuǎn)化
//1.1
//systemMsgFlag參數(shù)不要空格 后臺(tái)沒(méi)做判斷
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日
一.類(lèi)型轉(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日
一.查找第一個(gè)符合條件的值 設(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單個(gè)
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日
一.給類(lèi)擴(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.使用(注意對(duì)應(yīng)項(xiàng)一定要不為nil瞻讽,不然斷點(diǎn)進(jìn)不去熏挎,所以上面的本意默認(rèn)添加nil保護(hù)其實(shí)不起作用)
NSString *testID = _testExerciseId.protect; //不會(huì)調(diào)用 因?yàn)開(kāi)testExerciseId為nil
NSString *test2 = @"hello";
NSString *tmp = test2.protect;
如果您發(fā)現(xiàn)本文對(duì)你有所幫助,如果您認(rèn)為其他人也可能受益婆瓜,請(qǐng)把它分享出去。