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

2

6

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

    // ...
}
Carlottacarlovingian answered 3/7, 2015 at 19:18 Comment(1)
"I have a single codebase that needs to be compatible with Xcode 7 beta and Xcode 6.4" It won't be. Forget it. These are two completely different versions of Swift (and of the Cocoa API); you can't possibly write Swift code that works for both.Subsonic
B
2

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

Babcock answered 22/8, 2015 at 21:2 Comment(0)
G
1

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.

Gibran answered 22/8, 2015 at 21:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.