Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an #ifdef to distinguish between Xcode 6.4 and Xcode 7 beta in Swift?

I have a single codebase that needs to be compatible with Xcode 7 beta and Xcode 6.4. This is because beta testing and App Store builds should be built with the stable version of the compiler and SDK, but I also have iOS 9 beta on a phone I use for testing.

This hasn't been a problem with Objective-C, but now that I'm adding a bit of Swift, I'm finding it hard to maintain compatibility with both version of Xcode.

What can I do?

I know Swift has an #ifdef directive, but are there #ifdefs than can distinguish between Swift 1.2 and 2.0? I can't find a list of valid ones for Swift except for DEBUG, os, and arch.

Here's an example of what I mean:

#ifdef __IPHONE_9_0
    some Swift code that works in Swift 2.0 but won't compile in Swift 1.2
#else
    some Swift code that works in Swift 1.2 but won't compile in Swift 2.0
#endif

Or a more concrete example:

public final class MessageParser : NSObject {

    #ifdef __IPHONE_9_0
    static let sharedHashtagRegex = try! NSRegularExpression(pattern:"(^|\\W)(#|\\uFF03)(\\w*\\p{L}\\w*)", options:[]);
    #else
    static let sharedHashtagRegex = NSRegularExpression(pattern:"(^|\\W)(#|\\uFF03)(\\w*\\p{L}\\w*)", options:nil, error:nil)!
    #endif

    // ...
}
like image 439
Sterling Christensen Avatar asked Jul 03 '15 19:07

Sterling Christensen


2 Answers

no, as Swift imposes all "define" path must compile. sorry but old good times of #ifdef are gone

like image 90
ingconti Avatar answered Sep 30 '22 19:09

ingconti


The problem is not so uncommon. We see it every time when we are in the middle of a heavy code refactoring. What can you do? Use a versioning system, e.g. git.

With git:

Swift 1.2 (Xcode 6.4) - keep the old code at your master branch

Swift 2.0 (Xcode 7.0) - put the migrated code to a new branch, e.g. features/swift20.

Now, for a while you will have to mantain two branches but that shouldn't be hard. You can add your changes to master and then merge them to features/swift20, solving all the collisions/migrations during the merge.

When Xcode 9 GM comes out, you can just merge features/swift20 into master because you won't need Swift 1.2 any more.

like image 32
Sulthan Avatar answered Sep 30 '22 19:09

Sulthan