Do you need help on a specific subject? Use the contact form (Request a blog entry) on the right hand side.

2017-01-05

Conditional compilation in Swift

There are many 'hits' when I google for 'swift conditional compilation'. But most of them are old and do not give a full example of just how to do this.

So here it is:

Btw, this works for Xcode 8.2.1

The first thing to know is that the simple approach only tests for the existence of definitions, not on the value of a definition.

A definition is simply a "word" that is known or not-known at compile time, by the compiler. Probably for this reason Apple calls them "Active Compiler Condition".

Active Compiler Conditions are defined in Xcode:



Here you can see that I defined an Active Compiler Condition with the name "USE_OPEN_SSL".

The active compiler condition can be used in the code like this:

        public func startReceiverLoop() {
            
            let queue = receiverQueue ?? DispatchQueue(label: "Receiver queue", qos: receiverQueueQoS, attributes: [], autoreleaseFrequency: DispatchQueue.AutoreleaseFrequency.inherit, target: nil)
            
            queue.async {
                
                [weak self] in
                
                switch self?.type {
                
                case nil: fatalError("No receiver type specified")
                
                case let .socket(sock)?:
                    
                    SwifterSockets.receiveLoop(
                        socket: sock,
                        bufferSize: self?.receiverBufferSize ?? 20 * 1024,
                        duration: self?.receiverLoopDuration ?? 5,
                        receiver: self)
                    
                
                case let .ssl(ssl, _)?:
                    
                    #if USE_OPEN_SSL
                        
                        SwifterSockets.Ssl.receiveLoop(
                            ssl: ssl,
                            bufferSize: self?.receiverBufferSize ?? 20 * 1024,
                            duration: self?.receiverLoopDuration ?? 5,
                            receiver: self)
                    #else
                        break
                    #endif
                }
            }

        }

If the ACC USE_OPEN_SSL definition is present, the compiler will compile the code as:

                case let .ssl(ssl, _)?:
                                            
                        SwifterSockets.Ssl.receiveLoop(
                            ssl: ssl,
                            bufferSize: self?.receiverBufferSize ?? 20 * 1024,
                            duration: self?.receiverLoopDuration ?? 5,
                            receiver: self)
                }

If however the definition is absent, the compiler will compile the code as:

                case let .ssl(ssl, _)?:
                    
                        break
                }

Btw, this is indeed a preview of an upcoming release of SwifterSockets that will support secure connections. See the links on the right hand side for a link to the current edition of SwifterSockets on github.

Happy coding...

Did this help?, then please help out a small independent.
If you decide that you want to make a small donation, you can do so by clicking this
link: a cup of coffee ($2) or use the popup on the right hand side for different amounts.
Payments will be processed by PayPal, receiver will be sales at balancingrock dot nl
Bitcoins will be gladly accepted at: 1GacSREBxPy1yskLMc9de2nofNv2SNdwqH

We don't get the world we wish for... we get the world we pay for.

No comments:

Post a Comment