Objective-C Tips

1、oc中各種nil。
  1. nil
    • 用來表示空對象,數(shù)組、字典結(jié)束判斷
  2. Nil
    • 用于類的空指針
  3. NSNull
    • 用來標識什么都沒有 [NSNull null],通常用于集合占位
  4. NULL
    • 通用用于c指針變量為空
      NSLog輸出nil、Nil為(null),輸出NSNull為<null>

2、判斷兩個日期是否在同一周

// 方法一
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
        
        NSString *todayString = @"2019-10-07 03:02:00";
        NSString *afterString = @"2019-10-06 00:08:33";
        
        // 方法一
        NSDate *today = [formatter dateFromString:todayString];
        NSDate *afterDay = [formatter dateFromString:afterString];
        
        formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss E";
        NSLog(@"今天: %@", [formatter stringFromDate:today]);
        NSLog(@"之前: %@", [formatter stringFromDate:afterDay]);
        
        NSInteger weekday = [[[NSCalendar currentCalendar] components:NSCalendarUnitWeekday fromDate:today] weekday];
        NSInteger afterweekday = [[[NSCalendar currentCalendar] components:NSCalendarUnitWeekday fromDate:afterDay] weekday];
        weekday = weekday == 1 ? 7 : weekday - 1;
        afterweekday = afterweekday == 1 ? 7 : afterweekday - 1;
        NSLog(@"今天周 : %ld, 之前是: %ld",(long)weekday, afterweekday);

        NSInteger startDay=[NSCalendar.currentCalendar ordinalityOfUnit:NSCalendarUnitDay inUnit: NSCalendarUnitYear forDate:afterDay];
        NSInteger endDay=[NSCalendar.currentCalendar ordinalityOfUnit:NSCalendarUnitDay  inUnit: NSCalendarUnitYear forDate:today];
        NSLog(@"startDay : %ld,  endDay: %ld ", (long)startDay, (long)endDay);
         NSInteger distance = endDay - startDay;

        NSLog(@"test : today: %@,  afterDay: %@ %ld", today, afterDay, (long)distance);
        if (labs(distance) > 6) {
            NSLog(@"不在同一周");
        } else if (distance > 0) {
            if (distance < weekday) {
                NSLog(@"在同一周");
            } else {
                NSLog(@"不在同一周");
            }
        } else {
            if (labs(distance) <= 7 - weekday) {
                NSLog(@"在同一周");
            } else {
                NSLog(@"不在同一周");
            }
        }

        // 方法二(推薦使用)
        
        NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
        calendar.firstWeekday = 2;  // 周一是一周的開始第一天
        
        NSCalendarUnit units = NSCalendarUnitWeekday | NSCalendarUnitYear | NSCalendarUnitWeekOfYear;
        
        NSDateComponents *todayComp = [calendar components:units fromDate:today];
        NSDateComponents *afterComp = [calendar components:units fromDate:afterDay];
        
        NSLog(@"%@, \n %@ \n %@", todayComp, afterComp);
        
        if (todayComp.year == afterComp.year && todayComp.weekOfYear == afterComp.weekOfYear) {
            NSLog(@"在同一周");
        } else{
            NSLog(@"不在同一周");
        }

3、Objective-C 中 nullable、__nullable、_Nullable 的區(qū)別

  • 最開始使用的是__nullable,但是由于可能與第三方庫沖突,蘋果引進勒_Nullable,而且蘋果支持不帶下劃線寫法nullable
- (nullable NSString *)method;
- (NSString * __nullable)method;
- (NSString * _Nullable)method;
  • 對于 雙指針類型對象 、Block 的返回值、Block 的參數(shù) 等,這時候就不能用 nonnull/nullable 修飾,只能用帶下劃線的 __nonnull/__nullable 或者 _Nonnull/_Nullable:
  • 蘋果建議,對于屬性、方法返回值、方法參數(shù)的修飾,使用:nonnull/nullable;
    對于 C 函數(shù)的參數(shù)、Block 的參數(shù)、Block 返回值的修飾,使用:_Nonnull/_Nullable,建議棄用 __nonnull/__nullable。
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容