1、初始化NSAttributedString时要校验string是否为空,为空会导致闪退。
NSAttributedString *str = [[NSAttributedString alloc] initWithString:nil attributes:@{NSForegroundColorAttributeName:[UIColor redColor]}];
2、下面的代码是错误的,当someString有值时是正确的,当等于nil时,会出现的错误的结果。
NSString *someString = nil;
NSRange range = [someString rangeOfString:@"hello"];
NSLog(@"[someString=%@,range=%@]", someString, NSStringFromRange(range));
if (range.location != NSNotFound) {
NSLog(@"find hello!");
}
someString = nil时会得到下面的输出:
[someString=(null),range={0, 0}]
find hello!
3、判断NSValue的具体的数据类型
NSValue *value = [NSValue valueWithCGPoint:CGPointMake(10, 10)];
BOOL isCGPoint = strcmp(value.objCType, @encode(CGPoint)) == 0;
同样的,
BOOL isCGSize = strcmp(value.objCType, @encode(CGSize)) == 0;
BOOL isCGRect = strcmp(value.objCType, @encode(CGRect)) == 0;
BOOL isUIEdgeInsets = strcmp(value.objCType, @encode(UIEdgeInsets)) == 0;
BOOL isUIOffset = strcmp(value.objCType, @encode(UIOffset)) == 0;
BOOL isCGAffineTransform = strcmp(value.objCType, @encode(CGAffineTransform)) == 0;
BOOL isCGVector = strcmp(value.objCType, @encode(CGVector)) == 0;