iOS开发小知识

__has_include
#if __has_include()
FOUNDATION_EXPORT double YYModelVersionNumber;
FOUNDATION_EXPORT const unsigned char YYModelVersionString[];
#import 
#import 
#else
#import "NSObject+YYModel.h"
#import "YYClassInfo.h"
#endif

  • 刷新标记
[self setNeedsLayout] //标记刷新标识
[self layoutIfNeed] // 检测刷新标识,如果有就立即刷新
  • 隐藏警告信息
#pragma clang diagnostic push
#pragma clang diagnostic warning "-Wimplicit-retain-self"
//去除⚠️
#pragma clang diagnostic pop

#define SuppressPerformSelectorLeakWarning(Stuff) \
do { \
_Pragma("clang diagnostic push") \
_Pragma("clang diagnostic ignored \"-Warc-performSelector-leaks\"") \
Stuff; \
_Pragma("clang diagnostic pop") \
} while (0)
  • 为什么要使用协议
    内部不要写死,展示结果最好由外部决定,
    UITableview只要外界遵守数据源协议就能展示数据
    UIApplication 有些事情不想去实现 ,谁实现谁就帮我监听
抛异常
//方式1
    NSException *exception = [NSException exceptionWithName:@"fileError" reason:@"文件夹路径不存在" userInfo:nil];
    [exception raise];
//方式2
 @throw [NSException exceptionWithName:@"fileError" reason:@"文件夹路径不存在" userInfo:nil];
数组元素去重
weakSelf.groupIDArray = [weakSelf.groupIDArray valueForKeyPath:@"@distinctUnionOfObjects.self"];
获取当前周开始和结束的日期
    NSDate *now = [NSDate date];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *comp = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitWeekday|NSCalendarUnitDay
                                         fromDate:now];
    NSInteger weekDay = [comp weekday];
    // 得到几号
    NSInteger day = [comp day];
    // 计算当前日期和这周的星期一和星期天差的天数,计算出这周开始和结束的日期
    long firstDiff,lastDiff;
    if (weekDay == 0) {
        firstDiff = 0;
        lastDiff = 0;
    }else{
        firstDiff = 1 - weekDay;
        lastDiff = 7 - weekDay;
    }
    
    NSLog(@"firstDiff:%ld   lastDiff:%ld",firstDiff,lastDiff);
    
    // 在当前日期(去掉了时分秒)基础上加上差的天数
    NSDateComponents *firstDayComp = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitWeekday|NSCalendarUnitDay
                                                 fromDate:now];
    [firstDayComp setDay:day + firstDiff];
    //获取这周开始的日期
    NSDate *firstDayOfWeek= [calendar dateFromComponents:firstDayComp];
    NSDateComponents *lastDayComp = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitWeekday|NSCalendarUnitDay
                                                fromDate:now];
    [lastDayComp setDay:day + lastDiff];
    //获取这周结束的日期
    NSDate *lastDayOfWeek= [calendar dateFromComponents:lastDayComp];
    NSDateFormatter *formater = [[NSDateFormatter alloc] init];
    [formater setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSLog(@"星期天开始 %@",[formater stringFromDate:firstDayOfWeek]);
    NSLog(@"星期六结束 %@",[formater stringFromDate:lastDayOfWeek]);

你可能感兴趣的:(iOS开发小知识)