恼人的警告:整数常量不在枚举类型'UIViewAnimationOptions'的范围内

Tim*_*Tim 16 c xcode objective-c ios

使用clang设置为C11/C++ 11在XCode 5中编写如下代码时:

[UIView animateWithDuration:0.5
    delay:0
    options:UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat
    animations:^{
        self.imgCheckIn.backgroundColor = [UIColor redColor];
    }
    completion:nil];
Run Code Online (Sandbox Code Playgroud)

该options字段生成以下警告:

integer constant not in range of enumerated type 'UIViewAnimationOptions' (aka 'enum UIViewAnimationOptions') [-Wassign-enum]
Run Code Online (Sandbox Code Playgroud)

问题似乎是该方法采用了一种UIViewAnimationOptions类型,这只是一个枚举NSUInteger.但是,OR'ing值一起创建一个未在枚举中明确显示的值,因此它会抱怨.

一般来说,这似乎是一个很好的警告,所以我想保留它.难道我做错了什么?

Mar*_*n R 33

你没有做错任何事.正如您已经注意到的那样,编译器会抱怨,因为该值不是枚举中定义的值.(编译器标志-Weverything意味着这个检查.)

您可以通过显式强制转换来抑制警告:

options:(UIViewAnimationOptions)(UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat) 
Run Code Online (Sandbox Code Playgroud)

或者#pragma:

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wassign-enum"
[UIView animateWithDuration:0.5
                      delay:0
                    options:UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat
                 animations:^{
                     self.imgCheckIn.backgroundColor = [UIColor redColor];
                 }
                 completion:nil];
#pragma clang diagnostic pop
Run Code Online (Sandbox Code Playgroud)

  • 是的,我正在寻求更好的解决方案:) (5认同)