收录一些优雅的写法:
1.
- (BOOL)resignFirstResponder
{
[self.textView resignFirstResponder];
return [super resignFirstResponder];
}
因为resignFirstResponder是默认返回YES的,所以super的时候做了这个操作。
另外在这个方法里面更新一些控件的状态。苹果说得很清楚了!
2.
NSString * tmpGameID = isStringEmpty(gameID)?@"":gameID;
判空,不用写啥if啦
3.
BOOL result = [SingleAPIProtal bundleLoadDylibWithPath:framework]; // 定义布尔判空.
+ (BOOL)bundleLoadDylibWithPath:(NSString *)path // 封装一个函数体外的判空方法.
{
NSError *err = nil;
NSBundle *bundle = [NSBundle bundleWithPath:path];
if ([bundle loadAndReturnError:&err]) {
NSLog(@"[SingleAPIProtal bundleLoadDylibWithPath:] bundle load framework success.");
return YES;
} else {
NSLog(@"[SingleAPIProtal bundleLoadDylibWithPath:] bundle load framework err:%@",err);
return NO;
}
}
4.
DTCXMLResponse
这种命名也是挺帅的。
5.
#define SYSTEM_VERSION_IS_8_0_X ([[[UIDevice currentDevice] systemVersion] hasPrefix:@"8.0"])
6.
判断布尔的时候,来个这些操作也是不错的
- (BOOL)labelShouldScroll
{
BOOL stringLength = ([self.subLabel.text length] > 0);
if (!stringLength)
{
return NO;
}
BOOL labelTooLarge = ([self subLabelSize].width + self.leadingBuffer > self.bounds.size.width);
return (!self.labelize && labelTooLarge);
}
BOOL trailingFadeNeeded = (!self.labelize || self.labelShouldScroll);
7.
宏来定义多个表达式
#define INSTANCE_XSDK() Class XSDKClass = NSClassFromString(@"XSDK"); \
SEL instanceXSDKSel = NSSelectorFromString(@"instanceXSDK"); \
id instanceXSDK = ((id(*)(id, SEL))objc_msgSend)(XSDKClass, instanceXSDKSel);
包括这种:
ACKit_EXTERN id<ACAccessChannel> ACStandardChannel();
看起来超级复杂,其实也是前面做了一个宏处理而已。让代码看起来更加精简。
8.
if (VERSION_FLOAT >= 7.0 && [tipText respondsToSelector:@selector(sizeWithAttributes:)])
{
}