Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 305 Vote(s) - 3.6 Average
  • 1
  • 2
  • 3
  • 4
  • 5
#ifdef replacement in the Swift language

#11

[![In Xcode 8 & above go to build setting -> search for custom flags
][1]][1]

[1]:



In code

#if Live
print("Live")
#else
print("debug")
#endif
Reply

#12
## isDebug Constant Based on Active Compilation Conditions ##

Another, perhaps simpler, solution that still results in a boolean that you can pass into functions without peppering `#if` conditionals throughout your codebase is to define `DEBUG` as one of your project build target's `Active Compilation Conditions` and include the following (I define it as a global constant):

#if DEBUG
let isDebug = true
#else
let isDebug = false
#endif



[1]:

[To see links please register here]



----------


## isDebug Constant Based on Compiler Optimization Settings ##

This concept builds on [kennytm's answer][1]

The main advantage when comparing against kennytm's, is that this does not rely on private or undocumented methods.

In **Swift 4**:

let isDebug: Bool = {
var isDebug = false
// function with a side effect and Bool return value that we can pass into assert()
func set(debug: Bool) -> Bool {
isDebug = debug
return isDebug
}
// assert:
// "Condition is only evaluated in playgrounds and -Onone builds."
// so isDebug is never changed to true in Release builds
assert(set(debug: true))
return isDebug
}()

Compared with preprocessor macros *and kennytm's answer*,

* ✓ You don't need to define a custom `-D DEBUG` flag to use it
* ~ It is actually defined in terms of optimization settings, not Xcode build configuration
* ✓ **Documented**, which means the function will follow normal API release/deprecation patterns.

* ✓ Using in if/else will **not** generate a "Will never be executed" warning.
Reply

#13
As of Swift 4.1, if all you need is just check whether the code is built with debug or release configuration, you may use the built-in functions:

* `_isDebugAssertConfiguration()` (true when optimization is set to `-Onone`)
* <del>`_isReleaseAssertConfiguration()` (true when optimization is set to `-O`)</del> (not available on Swift 3+)
* `_isFastAssertConfiguration()` (true when optimization is set to `-Ounchecked`)

e.g.

func obtain() -> AbstractThing {
if _isDebugAssertConfiguration() {
return DecoratedThingWithDebugInformation(Thing())
} else {
return Thing()
}
}

Compared with preprocessor macros,

* ✓ You don't need to define a custom `-D DEBUG` flag to use it
* ~ It is actually defined in terms of optimization settings, not Xcode build configuration
* ✗ Undocumented, which means the function can be removed in any update (but it should be AppStore-safe since the optimizer will turn these into constants)

* these once [removed](

[To see links please register here]

), but [brought back to public to lack of `@testable` attribute](

[To see links please register here]

), fate uncertain on future Swift.

* ✗ Using in if/else will always generate a "Will never be executed" warning.
Reply

#14
In Swift projects created with Xcode Version 9.4.1, Swift 4.1

#if DEBUG
#endif
works by default because in the Preprocessor Macros DEBUG=1 has already been set by Xcode.

So you can use #if DEBUG "out of box".

By the way, how to use the condition compilation blocks in general is written in Apple's book The Swift Programming Language 4.1 (the section Compiler Control Statements) and how to write the compile flags and what is counterpart of the C macros in Swift is written in another Apple's book Using Swift with Cocoa and Objective C (in the section Preprocessor Directives)

Hope in future Apple will write the more detailed contents and the indexes for their books.
Reply

#15
func inDebugBuilds(_ code: () -> Void) {
assert({ code(); return true }())
}

[Source][1]


[1]:

[To see links please register here]

Reply

#16
There are some processors that take an argument and I listed them below. you can change the argument as you like:

```
#if os(macOS) /* Checks the target operating system */

#if canImport(UIKit) /* Check if a module presents */

#if swift(<5) /* Check the Swift version */

#if targetEnvironment(simulator) /* Check envrionments like Simulator or Catalyst */

#if compiler(<7) /* Check compiler version */
```

Also, You can use any custom flags like `DEBUG` or any other flags you defined
```
#if DEBUG
print("Debug mode")
#endif
```
Reply

#17
Moignans [answer][1] here works fine. Here is another piece of info in case it helps,

#if DEBUG
let a = 2
#else
let a = 3
#endif

You can negate the macros like below,

#if !RELEASE
let a = 2
#else
let a = 3
#endif




[1]:

[To see links please register here]

Reply

#18
Swift 5 update for [matt's answer](

[To see links please register here]

)

```
let dic = ProcessInfo.processInfo.environment
if dic["TRIPLE"] != nil {
// ... do your secret stuff here ...
}
```
Reply

#19
**XCODE 9 AND ABOVE**

#if DEVELOP
//print("Develop")
#elseif PRODUCTION
//print("Production")
#else
//
#endif
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through