Xcode消除编译器警告

Whenever,Xcode警告对于我们来说都相当重要,提醒我们可能存在的错误。但是有时候,我们知道一切都好,everything is in the palm of my hand,我们想要消除那些警告。

自己项目的警告

比如我们定义一个designated initializer,参数都是nonnull

- (instancetype)initWithGivenName:(NSString *)givenName familyName:(NSString *)familyName NS_DESIGNATED_INITIALIZER;
- (instancetype)initWithGivenName:(NSString *)givenName familyName:(NSString *)familyName
{
    NSAssert(givenName && familyName, @"givenName and familyName must not be nil");

    if (self = [super init]) {

        self.givenName = givenName;
        self.familyName = familyName;

    }
    return self;
}

当实现父类的designated initializer时,调用上面这个方法并且传入参数为nil。显然,编译器对此产生警告

- (instancetype)init
{
    return [self initWithGivenName:nil familyName:nil];
}

FE0ABDC9-1B8A-46E1-B830-4DB49726380F.png
  • 找到警告类型

右击警告,选择在日志中显示


63568C18-C326-414F-96A5-DA1A80792933.png

-W+警告类型 : 意味着打开某种类型的警告,-Wnonnull打开nonnull类型的警告
-Wno-+警告类型 : 意味着关闭某种类型的警告,-Wno-nonnull关闭nonnull类型的警告


69960C85-AA3C-4DA7-B1CA-BAD90A4A7963.png
  • 忽略某些行警告,被宏包裹的行忽略指定警告
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wnonnull"
- (instancetype)init
{
    return [self initWithGivenName:nil familyName:nil];
}
#pragma clang diagnostic pop

值得注意的是 : 这里是-Wnonnull

  • 禁用某个文件警告

08DCEE87-30B4-4842-BF29-E15FAF28B564.png
  • 禁用某个target警告

4B118D04-8E44-4DB7-89BB-28B122A5815F.png

第三方库的警告

当我们使用CocoaPods管理库依赖时,可以通过Podfile语法禁止第三方库的警告

CocoaPods管理AFNetworking和SDWebImage

# Uncomment the next line to define a global platform for your project
platform :ios, '9.0'

target 'CocoaPodsDemoWithOC' do
  # Uncomment the next line if you're using Swift or would like to use dynamic frameworks
  # use_frameworks!

  # Pods for CocoaPodsDemoWithOC
  pod 'AFNetworking', '2.5.0'
  pod 'SDWebImage', '3.7.5'
end
  • 禁止所有来自CocoaPods的警告
    inhibit_all_warnings!
# Uncomment the next line to define a global platform for your project
platform :ios, '9.0'
inhibit_all_warnings!

target 'CocoaPodsDemoWithOC' do
  # Uncomment the next line if you're using Swift or would like to use dynamic frameworks
  # use_frameworks!

  # Pods for CocoaPodsDemoWithOC
  pod 'AFNetworking', '2.5.0'
  pod 'SDWebImage', '3.7.5'
end
  • 禁止指定Pod的警告

这里禁止AFNetworking的警告

# Uncomment the next line to define a global platform for your project
platform :ios, '9.0'

target 'CocoaPodsDemoWithOC' do
  # Uncomment the next line if you're using Swift or would like to use dynamic frameworks
  # use_frameworks!

  # Pods for CocoaPodsDemoWithOC
  pod 'AFNetworking', '2.5.0', :inhibit_warnings => true
  pod 'SDWebImage', '3.7.5'
end
  • 禁止除了指定Pod之外的警告

这里禁止SDWebImage的警告

# Uncomment the next line to define a global platform for your project
platform :ios, '9.0'
inhibit_all_warnings!

target 'CocoaPodsDemoWithOC' do
  # Uncomment the next line if you're using Swift or would like to use dynamic frameworks
  # use_frameworks!

  # Pods for CocoaPodsDemoWithOC
  pod 'AFNetworking', '2.5.0', :inhibit_warnings => false
  pod 'SDWebImage', '3.7.5'
end

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值