Is Xcode9 backwards compatible to Swift 3 (or 3.1)? - swift

I've just started using Xcode9 (beta2). My understanding was that it wouldn't force me to immediately change my code. However, I'm getting a build errors coming up with a library I'm using (Gloss):
1) For the following code:
public func valueForKeyPath(keyPath: String, withDelimiter delimiter: String = GlossKeyPathDelimiter, logger: Logger = GlossLogger()) -> Any? {
I get:
Initializer 'init()' is internal and cannot be referenced from a
default argument value
2) For this code:
internal func flatMap<KeyPrime : Hashable, ValuePrime>(_ transform: (Key, Value) throws -> (KeyPrime, ValuePrime)?) rethrows -> [KeyPrime : ValuePrime] {
return Dictionary<KeyPrime,ValuePrime>(elements: try flatMap({ (key, value) in
return try transform(key, value)
}))
}
I'm getting:
Closure tuple parameter '(key: _, value: _)' does not support
destructuring
Both of these caused no problem with Xcode 8.3/Swift 3.1.
I am using Xcode9 without having chosen the option to update to Swift 4. There seems several possibilities:
There are some flags that need to be set to retain Swift 3.1 backwards compatibility
Xcode9 is in beta and just isn't ready fully
Xcode9 will not be completely backwards compatible to Swift 3.1
Thoughts?

Xcode 9 is cross compatible with Swift 3 and 4. However, you need to be on Swift 3.2 which is the minimum for Xcode 9.

You can find, list of languages supported by specific Xcode, from Build Settings of your Xcode Project.
Here is sample reference, how you can see it:
(For Xcode 9, it supports, Swift 3.2 and Swift 4.0)
Here is reference answer, how you can easily switch from Swift 3.0 to 4.0
XCode - upgrade to Swift 4 manually

Related

Xcode 10.2 - Use of undeclared type Result

I have downloaded Xcode 10.2 beta release. I am trying to use new Result type in Application project or Playground with Swift 5 enabled.
My code is as below:
import UIKit
enum SampleError: Error {
case foo
}
func bar() -> Result<Int, SampleError> {
}
When compiling I am getting:
Use of undeclared type 'Result'
I have double checked settings and verified with below code that I am using Swift 5.0
#if swift(>=5.0)
print("Hello, Swift 5.0")
#endif
Does anybody encountered similar issue?
Update:
Result is now available with Xcode 10.2 beta 2 release. Yay
Here is the link to release note where you can find more details about Swift changes.

Swift version build configuration

With Swift v4.2 they introduced the extension Bool.toggle(), I had this extension since earlier and now when I compile with Xcode10 it says Ambiguous use of 'toggle()'. I tried to make it ignore my own extension if Swift version is 4.2 or higher so I used the build configuration swift(>=) which should check if the current swift version is equal or higher than the specified version.
public extension Bool {
#if swift(>=4.2)
// DO NOTHING
#else
mutating func toggle() {
self = !self
}
#endif
}
This should only see the comment DO NOTHING when on Xcode10 and on Xcode9 it should see the extended method. But whats happening is when on Xcode10 it still sees the method and I still get the error "Ambiguous use" and on Xcode9 it sees the method as well. I also changed the Build Setting Swift Language Version to Swift 4.2 and still the compiler sees the method.

Segmentation fault in Swift compiler when converting to Swift 4 syntax

I have some Swift code that was written in Swift 3.2, and I just attempted to use the automatic syntax converter in Xcode 9.0 to update to Swift 4.0.
It actually found nothing that needed to be converted, but during the code analysis when the compiler compiles the project, the compiler crashes with a segmentation fault. I can't share the full source code, so I created a simple example which demonstrates the same issue, and confirmed that this also produces a crash in a newly-created Swift project.
The compiler crash only appears when joining values together using string interpolation. To reproduce:
Create a new Xcode project, and set Swift version to 3.2
Create a new Swift class
Paste the following code into the new class:
public class SomeClass: NSObject {
static let defaultStringPrefix = "abc"
var a: String = ""
var b: String = ""
var c: String = ""
lazy var concatStr: String = {
let str = "\(defaultStringPrefix)/\(self.a)/\(self.b)/\(self.c)/somepage.html"
return str
}()
}
Attempt to perform the Swift 3.2 to Swift 4.0 conversion -> This will fail and a segfault will appear with the following error:
Command failed due to Segmentation fault: 11
Stack dump:
0....(a huge load of arguments to the swift compiler)
1. While type-checking getter for concatStr at /Users/craigrouse/mobiledev/test/test/SomeClass.swift:17:14
2. While type-checking declaration 0x7ff292a86400 in module 'test'
3. While type-checking expression at [/Users/craigrouse/mobiledev/test/test/SomeClass.swift:17:34 - line:20:7] RangeText="{
let str = "\(a)/\(b)/\(c)/somepage.html"
return str
}()"
I've tried various versions of this, and if I remove the string interpolation, the problem goes away. Obviously I could avoid using the auto conversion, and set the Swift version to 4 manually in the project settings, but I'm sure others will encounter this mysterious error, and I'd like to find out what's causing it. Is there a problem with my syntax?
Thanks.

Cannot invoke "Int" with an argument of type "String"

When I use this code from apple book in my playground:
let possibleNumber = "123"
let convertedNumber = Int(possibleNumber)
Xcode shows an error:
Cannot invoke "Int" with an argument of type "String"
But compiler should refer convertedNumber as optional, as I understand.
You must be using Swift 1.x (in Xcode 6), but the Swift book is for the last version of Swift, currently 2.1 (in Xcode 7). The String initializer for Int() wasn't available in Swift 1.
I was able to make this work with no issues. It could be the XCode causing the issue. Try a reinstallation of the XCode.

GCC_VERSION equivalent for Swift code? [duplicate]

I know I can find the version of Swift I'm running right now reverting to a Terminal and typing:
xcrun swift --version
Swift version 1.1 (swift-600.0.57.4)
Target: x86_64-apple-darwin13.4.0
Also, I've been reading about the Preprocessor Macros in Swift, but no luck finding a Swift version constant.
As Swift 1.2 approaches it will be nice to flag old code that only runs on Swift 1.1 (Xcode up to 6.2) or new code that needs Xcode 6.3 (Swift 1.2)
Note: I can also use system() to do something like:
system("xcrun swift --version | grep version > somefile.txt")
Then open somefile.txt, but rather prefer some simpler solution
You can use conditional compilation directives to test for the specific Swift version used to build your project:
#if swift(>=5.0)
print("Hello, Swift 5!")
#elseif swift(>=4.0)
print("Hello, Swift 4!")
#elseif swift(>=3.0)
print("Hello, Swift 3!")
#elseif swift(>=2.2)
print("Hello, Swift 2.2!")
#elseif swift(>=2.1)
print("Hello, Swift 2.1!")
#endif
Finally got a workaround to do this. I'm using the constants prefixed with __ you can observe in your Playground. This would have been easier with some level of reflection, but...
__IPHONE_OS_VERSION_MAX_ALLOWED is 80200, meaning __IPHONE_8_2 for Xcode 6.2 (Swift 1.1) but its value is 80300 (__IPHONE_8_3) in Xcode 6.3 (Swift 1.2)
func isSwift12() -> Bool {
return __IPHONE_OS_VERSION_MAX_ALLOWED == 80300
}
isSwift12()
So now in your library you can fail fast and tell your user Swift's version is not correct using this:
assert(isSwift12(), "Need Swift 12")
Swift will give you a nice:
assertion failed: Need Swift 12: file , line 20
UPDATE WWDC 2015 - Swift 2.0
As stated in Apple's Swift blog, in Swift 2.0 we have #available blocks to check for certain OS versions in our code. An example should be:
if #available(OSX 10.11, *) {
monochromeFilter!.setValue(CIColor(red: 0.5, green: 0.5, blue: 0.5), forKey:kCIInputColorKey)
} else {
// Fallback on earlier versions
}
Swift 3.1 extends the #available attribute to support specifying Swift version numbers in addition to its existing platform versions.
// Swift 3.1
#available(swift 3.1)
func intVersion(number: Double) -> Int? {
return Int(exactly: number)
}
#available(swift, introduced: 3.0, obsoleted: 3.1)
func intVersion(number: Double) -> Int {
return Int(number)
}
From your comment:
I want to check because different versions of Swift have different features
You should not check the version of your programming language in order to use some features or not. This approach is much better:
if (self.respondsToSelector(Selector("yourMethodSelector"))) {
self.yourMethodSelector(test, sender: self)
} else {
//error handling
}
Just check whether a method is available or not.
Open up a command line on your Mac computer
type swift -version, without the double dashes, which doesn't work any more for Xcode 11/Swift 5.2.2
Of course, you'll have to update to latest IDE, which always downloads the latest version of both Xcode, languages, compilers and tools... EVERYTHING is updated. If you do that, that solution works for sure.
For iOS :
var systemVersion = UIDevice.currentDevice().systemVersion;
For OSX :
var systemVersion = NSProcessInfo.processInfo().operatingSystemVersion;
K.