3

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!

zeeple
  • 5,509
  • 12
  • 43
  • 71
  • May this help you - https://stackoverflow.com/questions/30790188/how-do-i-see-which-version-of-swift-im-using/46080904#46080904 – Krunal Oct 23 '17 at 09:33

3 Answers3

2

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)

ingconti
  • 10,876
  • 3
  • 61
  • 48
1

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.

Darko
  • 9,655
  • 9
  • 36
  • 48
  • How can you tell which version of swift is bundled with the version of Xcode you are running? There is nothing I could find in About Xcode or in preferences. – zeeple Apr 14 '16 at 14:57
1

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)
Zhao
  • 904
  • 1
  • 11
  • 34
  • I tried this solution. On my work computer, running Xcode 7.2.1, I get the error: "Unexpected target configuration expression (expected 'os' or 'arch')" – zeeple Apr 14 '16 at 14:54
  • 1
    ah, sorry I forgot Xcode 7.2.1 (has Swift 2.1.x) does not support `swift()`, it is only introduced in swift 2.2. I updated my answer for this. Please let me know if it works. You can see the Xcode release code here about the changes: https://developer.apple.com/library/mac/releasenotes/DeveloperTools/RN-Xcode/Chapters/Introduction.html – Zhao Apr 14 '16 at 15:04