- (Nsstring *)getAuthorizationheader{ iKMAppDelegate *delegate = (iKMAppDelegate *)[UIApplication sharedApplication].delegate; Nsstring *header = [Nsstring stringWithFormat:@"Bearer %@",delegate.appDataObject.oauth2Acesstoken]; return header;}
这个方法会在XCode9中收到警告
[UIApplication delegate] must be called from main thread only
我不认为主队列中的调度将适用于我的功能.那么如何很好地解决这个警告呢?
解决方法 在主线程上无法访问UIApplication的委托,但您可以使用dispatch_sync轻松完成- (Nsstring *)getAuthorizationheader{ __block iKMAppDelegate *delegate; if([NSThread isMainThread]) { delegate = (iKMAppDelegate *)[UIApplication sharedApplication].delegate; } else { dispatch_sync(dispatch_get_main_queue(),^{ delegate = (iKMAppDelegate *)[UIApplication sharedApplication].delegate; }); } Nsstring *header = [Nsstring stringWithFormat:@"Bearer %@",delegate.appDataObject.oauth2Acesstoken]; return header;}
与dispatch_async相反,dispatch_sync函数将一直等到它传递的块完成后再返回.
使用dispatch_sync,有必要检查是否未从主线程执行该函数,这会导致死锁.
总结以上是内存溢出为你收集整理的iOS11 SDK无法访问应用程序的Delegate全部内容,希望文章能够帮你解决iOS11 SDK无法访问应用程序的Delegate所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)