What is accomplished by checking the address of a constant like SKStoreProductParameterAffiliateToken?

Viewed 63

I have this code from a library I am using.

#ifdef __IPHONE_8_0
    if (&SKStoreProductParameterAffiliateToken) {
        if (self.affiliateToken) {
            [appParameters setObject:self.affiliateToken forKey:SKStoreProductParameterAffiliateToken];
            if (self.campaignToken) {
                [appParameters setObject:self.campaignToken forKey:SKStoreProductParameterCampaignToken];
            }
        }
    }
#endif

Xcode is saying that the first line will always evaluate to be true but what is this line doing exactly? I never saw a if with & and a constant in that way.

SKStoreProductParameterAffiliateToken is defined as

SK_EXTERN NSString * const SKStoreProductParameterAffiliateToken NS_AVAILABLE_IOS(8_0);

What is the developer trying to check, the address of a constant? Is he trying to check if the version of iOS has this constant defined and by doing that, he is trying to check the instruction inside the if should run? But he already has ifdef __IPHONE_8_0... (??!!)

I don't get it.

Anyway I am compiling for iOS 9.3, so I can delete the if and the ifdef, right?

2 Answers

It is a check to see if a weak-linked symbol is available. If the library/framework containing the symbol has been weakly linked and is not available its address will evaluate to NULL and the if condition will be false.

See Using Weakly Linked Methods, Functions, and Symbols in Apple's Using SDK-Based Development for full details.

#ifdef __IPHONE_8_0 checks if Xcode should compile code inside. Ohterwise older version of Xcode will show an error about unknown variable SKStoreProductParameterAffiliateToken.

But when using newer Xcode version (with iOS SDK 8+), we may still can set a lower minimum target for our project. In this case to avoid crash on devices with lower than iOS 8 version we should check first if variable, class, method or function exists.

In your case, we are checking if pointer to SKStoreProductParameterAffiliateToken is not NULL, which means app is currently running at least on iOS 8.

Related