zeeple
zeeple

Reputation: 5617

How to Write a Conditional for Different Versions of Xcode

I am working on an app from two different computers. One is my home machine, which is an older MacBook Pro, but has the latest OS and is running Xcode 7.3. The second machine I use is my work machine, which is brand new and lightning fast, but is restricted to Yosemite and Xcode 7.2.1.

I recently encountered a build error on the machine running Xcode 7.2.1, but the app builds and runs without error on the machine running the newer Xcode. I cannot upgrade the work machine due to implacable IT policy, and I really (really) do not wish to downgrade my home machine to Xcode 7.2.1.

So what I would like to do is write a conditional similar to the following pseudocode:

if Xcode.version == 7.3
   // Run this version of the statement
   refreshControl.addTarget(self, action: #selector(ReadingTVC.pullToRefreshTableView), forControlEvents: UIControlEvents.ValueChanged)
if Xcode.version == 7.2.1
   // Run this different version of the statement
   // I still need to figure out how to rewrite the statement for 7.2.1

Is this possible? I found the following in Apple documentation, but there is no option for Xcode versions. only swift(), os() or arch():

https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/doc/uid/TP40014097-CH33-ID539

Thanks in advance!

Upvotes: 3

Views: 1331

Answers (3)

ingconti
ingconti

Reputation: 11646

little tip for example for annoying String history...

        #if swift(>=4.0)
            let len = text.count
        #else
            let len = text.characters.count
        #endif

works in Xcode 8.0 and higher (tested also in Xcode 9 beta)

Upvotes: 2

Zhao
Zhao

Reputation: 924

You should just use swift() to check the version of swift because Xcode 7.3 comes with Swift 2.2 and Xcode 7.2.1 comes with Swift 2.1.x.

For your code, since swift() is introduced in swift 2.2, you need to change use code that works in both versions, which is like below (Assuming ReadingTVC is self):

// For both swift 2.1.x and swift 2.2. In swift 2.2, this will pop a 
// warning and ask you to fix. But you can wait until you work computer
// updated Xcode version to 7.3. You will be force to change when 
// upgrade to swift 3.0 since the old string based api will be removed.
// Currently it is just marked as deprecated.
refreshControl.addTarget(self, action: "pullToRefreshTableView", forControlEvents: UIControlEvents.ValueChanged)

Upvotes: 1

Darko
Darko

Reputation: 9845

I can't test it currently but I think that 7.3 and 7.2.1 also have different Swift versions so you could use swift(). Apart from that I assume that the error is due to a change in Swift (the #selector syntax?) so this check would fit better anyway.

PS: instead of #selector(...) the older version just wants "pullToRefreshTableView" as selector.

Upvotes: 1

Related Questions