Home > Enterprise >  Swift equivelent for every 'x' press of button to perform action
Swift equivelent for every 'x' press of button to perform action

Time:09-28

Coming from objective-C background and just moving slowly into Swift, I can use let rather than #define however how would this statement be translated from objective-C to swift?

#define ADS_INTERSTITIAL_ON_LOBBY_FREQUENCY 5 will become let ADS_INTERSTITIAL_ON_LOBBY_FREQUENCY = 5

However, how can we translate this below to swift?

#ifdef ADS_INTERSTITIAL_ON_LOBBY_FREQUENCY

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSInteger nb_shown_lobby = [prefs integerForKey:@"nShownLobby"];
nb_shown_lobby  ;
[prefs setInteger:nb_shown_lobby forKey:@"nShownLobby"];
[prefs synchronize];

if ((nb_shown_lobby % ADS_INTERSTITIAL_ON_LOBBY_FREQUENCY) == 0) {
  
   //My Method goes here
    
}

#endif

The endgame of the above method is to basically show an action on every 5th press.

CodePudding user response:

You can probably assume that undefined variables are treated as 0 or false one way or the other, so checking against 0 should do the trick:

#if ADS_INTERSTITIAL_ON_LOBBY_FREQUENCY > 0

// Do something

#endif

Then you could probably manage ADS_INTERSTITIAL_ON_LOBBY_FREQUENCY via build configuration.

However you may also want to use entirely different approach here, such as for instance bundling a plist file that tells your application which features to enable and which to skip. This would be a runtime check but it's not that terrible.

The other way would be to introduce a struct with feature flags and compile the right configuration on per-target basis, i.e:

/// FeatureFlagsAbstract.swift
protocol FeatureFlags {
    static var myFeature: Int? { get }
}

/// Target A/Features.swift
struct MyFeatureFlags: FeatureFlags {
    static var myFeature: Int? {
        return 5
    }
}

/// Target B/Features.swift
struct MyFeatureFlags: FeatureFlags {
    static var myFeature: Int? {
        return nil
    }
}


/// Class.swift
class MyClass {
    func doSomething() {
       if let value = MyFeatureFlags.myFeature {
          // branch that uses some feature
       }
    }

}

CodePudding user response:

Unfortunately none of the provided replies gave a correct answer, I have since figured out the solution.

let ADS_INTERSTITIAL_ON_LOBBY_FREQUENCY = 5
                   
                    let prefs = UserDefaults.standard
                    var nb_shown_lobby = prefs.integer(forKey: "nShownLobby")
                    nb_shown_lobby  = 1
                    prefs.set(nb_shown_lobby, forKey: "nShownLobby")
                    prefs.synchronize()

                    if (nb_shown_lobby % ADS_INTERSTITIAL_ON_LOBBY_FREQUENCY) == 0 {
                        //Do your method here
                    }

CodePudding user response:

Has now it is a swift constant it can be processed with swift code :

if ADS_INTERSTITIAL_ON_LOBBY_FREQUENCY > 0 { // equivalent to ifdef 
…
} // équivalent to endif 

To undefined ADS_INTERSTITIAL_ON_LOBBY_FREQUENCY set its value to 0

In case value 0 may be used in other context, you may use optional integer :

// let ADS_INTERSTITIAL_ON_LOBBY_FREQUENCY : Int? = 5 
// -> is equivalent to #define myMacro 5
// let ADS_INTERSTITIAL_ON_LOBBY_FREQUENCY : Int? = nil 
// is equivalent to not defining the macro 
// uncomment the line according to what you want to achieve 

usage :

if let _ = ADS_INTERSTITIAL_ON_LOBBY_FREQUENCY {
    …
    if ((nb_shown_lobby % ADS_INTERSTITIAL_ON_LOBBY_FREQUENCY!) == 0) {

   //My Method goes here
    
}
    
}
  • Related