iOS知识备忘

  • 1、防止编译器警告
// completionBlock在AFURLConnectionOperation中被手动的设置为nil来打破保留周期。
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-retain-cycles"
    self.completionBlock = ^ {
        ...
    };
#pragma clang diagnostic pop
  • 2、手机系统时间显示佛教或日本日历后,显示时间错误问题
#ifdef __IPHONE_8_0
  NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];//根据Identifer获取公历
#else
  calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];//根据Identifer获取公历
#endif
  calendar.timeZone = [NSTimeZone localTimeZone];//消除时区差异
  calendar.locale = [NSLocale currentLocale];//消除本地化差异,本地化包括语言、表示的日期格式等 
  NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
  formatter.calendar = calendar; 
  • 3、锁的使用
static pthread_mutex_t _sharedDebugLock;
pthread_mutex_lock(&_sharedDebugLock);
...
pthread_mutex_unlock(&_sharedDebugLock);
dispatch_semaphore_t  framesLock = dispatch_semaphore_create(1);
dispatch_semaphore_wait(framesLock, DISPATCH_TIME_FOREVER);
...
dispatch_semaphore_signal(framesLock);
  • 4、循环引用问题
__weak typeof(self) weakSelf = self;
 self.testObject.testCircleBlock = ^{
      __strong typeof (weakSelf) strongSelf = weakSelf;
      [strongSelf doSomething];
}; 
  • 5、蓝牙耳机问题
    终端报AVAudioSessionPortImpl.mm:56:ValidateRequiredFields: Unknown selected data source for Port HiVi AW-71 (type: BluetoothHFP)
    原因:为了调优app设置,以更好的进行录音录像,从iOS7开始,在默认情况下,AVCaptureSession会使用app的AVAudioSession,并对其进行修改。这样,会发出AVAudioSessionInterruptionNotificationAVAudioSessionRouteChangeNotification通知。在录制视频时,如果前后台切换,就可能会发出这样的通知,在接收到AVAudioSessionInterruptionNotification通知时,在中断开始时暂停音乐播放,在中断结束时开启音乐播放。
  [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(handleInterruption:)
                                                     name:AVAudioSessionInterruptionNotification object:nil];
- (void)handleInterruption:(NSNotification*)notification {
    NSDictionary* userInfo = notification.userInfo; 
    if([userInfo[AVAudioSessionInterruptionTypeKey] intValue] == AVAudioSessionInterruptionTypeBegan) {
        [self.musicPlayer pause];
    } else {
        [self.musicPlayer play];
    }
}
  • 6、判断队列是否为当前执行队列
#define kSCRecorderRecordSessionQueueKey "SCRecorderRecordSessionQueue"

dispatch_queue_t sessionQueue = dispatch_queue_create("me.corsin.SCRecorder.RecordSession", nil); 
dispatch_queue_set_specific(sessionQueue, kSCRecorderRecordSessionQueueKey, "true", nil); 
dispatch_set_target_queue(sessionQueue, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0));
//判断当前执行队列是否为sessionQueue队列
+ (BOOL)isSessionQueue {
    return dispatch_get_specific(kSCRecorderRecordSessionQueueKey) != nil;
}
  • 7、常用的函数
celi():对一个数进行向上取整
floor():对一个数进行向下取整
round():四舍五入取整。

你可能感兴趣的:(iOS知识备忘)