Include an extension for a class only if iOS11 is available

无人久伴 提交于 2019-11-30 09:41:19

Xcode 9 beta 4 will build, if @available is added to each method. The build still fails if @available is added only at the extension level.

In ObjC, adding API_AVAILABLE(ios(11.0)) to end of the function definition will suppress the warning. Like this:

- (nullable UISwipeActionsConfiguration *)tableView:(UITableView *)tableView leadingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath API_AVAILABLE(ios(11.0))
{
     ... function implementation ...
}

You've done everything correctly, and the complaint is accurate if not extremely obtuse.

Basically, since you've (correctly) marked that function as available in iOS 11, the compiler will warn you about the use of it in any code that is targeting something < iOS 11.

So you can make the complaint go away by setting your deployment target to iOS 11, which means users of iOS 10 simply won't be allowed to install it. Or, you can use the new (to obj-c) @available construct to guard against the use of the API.

if (@available(iOS 11, *)) {
    [self setupDropInteractions];
}

This construct isn't supported by Xcode 8 since it's a recent back port of the #available construct provided by Swift.

Update

I'm clarifying where I'm coming from. It seems I haven't been able to reproduce the situation that the asker is experiencing, so I'm demonstrating what I have been able to do.

I can produce 2 different compiler warnings that are similar but it seems not identical to the original question.

Here is my generated objc interface from my_project-Swift.h:

@interface NoteEditViewController (SWIFT_EXTENSION(conditional_class_declaration)) <UIDropInteractionDelegate>
    - (UIDropProposal * _Nonnull)dropInteraction:(UIDropInteraction * _Nonnull)interaction sessionDidUpdate:(id <UIDropSession> _Nonnull)session SWIFT_WARN_UNUSED_RESULT SWIFT_AVAILABILITY(ios,introduced=11.0);
    - (void)setupDropInteractions SWIFT_AVAILABILITY(ios,introduced=11.0);
@end

Issue 1: Declaring and objc property to conform to the protocol

Issue 2: Using a method declared in the extension

With more information to reproduce the compilation error, I'll be able to help more.

Update 2: Hopefully the last

I found that the discrepancies in behavior between us was due to the fact that my project was created with Xcode 8.3 then migrated to 9. There seems to be a difference in build settings after these things happen. The setting in question is CLANG_WARN_UNGUARDED_AVAILABILITY, which I think is new for Xcode 9.

During migration the project ends up like this:

  • This maps to the term YES in the .pbxproject file

After new project creation:

  • This maps to the term YES_AGGRESSIVE is the .pbxproject file

This setting is discussed in WWDC2017 - What's new in LLVM, but nothing they say would suggest this minor behavioral difference. I'm guessing that it is a bug with clang and how it's handling the difference between the two settings (but I'd welcome other input). As I'm sure you've figured out, this code runs fine on iOS 10. Also, if you change the setting to simply "Yes", you will still get the correct warnings about iOS 11 APIs.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!