Marek H
Marek H

Reputation: 5566

Modern way to deal with deprecations on iOS/macOS

How to suppress deprecation warning within availability macro? I know availability is a great way to check for new APIs but I am struggling how to suppress deprecation warnings. What alternatives do I have other than those mentioned below? (1.pragma 2.performSelector)

MyModel *model;
if (@available(macOS 10.13, *)) {
    NSError *error;
    model = [NSKeyedUnarchiver unarchivedObjectOfClass:[MyModel class] fromData:metadata error:&error];
    if (error) {
        [[NSAlert alertWithError:error] runModal];
    }
} else {

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated"

    model = [NSKeyedUnarchiver unarchiveObjectWithData:metadata];

#pragma clang diagnostic pop
}

Alternatively use to suppress warning

if ([NSKeyedUnarchiver respondsToSelector:@selector(unarchiveObjectWithData:)]) {
    model = [NSKeyedUnarchiver performSelector:@selector(unarchiveObjectWithData:) withObject:metadata];
}

Upvotes: 1

Views: 481

Answers (1)

rmaddy
rmaddy

Reputation: 318774

You will only get a deprecation warning if you are using an API that was deprecated at or earlier than your target's Deployment Target.

NSKeyedUnarchiver unarchiveObjectWithData is deprecated as of macOS 10.14. You will only get a deprecation warning if your target's Deployment Target is macOS 10.14 or later. But the code you posted implies you wish to support macOS 10.12 or earlier.

NSKeyedUnarchiver unarchivedObjectOfClass:fromData:error: was added in macOS 10.13.

If you really only want a Deployment Target of macOS 10.13 or later then you don't need the if (@available(macOS 10.13, *)) or the else. Just use the new API and be done.

The code in your question (minus the pragmas) should only be used if you want to support macOS 10.12 or earlier. Then your target's Deployment Target needs to be set to macOS 10.12 or earlier. And in this case you don't need the pragmas and you won't get any deprecation warnings.

Upvotes: 2

Related Questions