GCC_VERSION equivalent for Swift code? [duplicate] - swift

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.

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.

Use of unresolved identifier: kCGWindowImageDefault (and other Core Graphics constants)

I'm new to Swift development. I'm writing an app for MacOS (not iOS) in Swift, and am trying to adapt some of the code from this Apple sample Objective-C project for use in my program.
The problem I'm hitting is that certain Apple-defined constants such as kCGWindowImageDefault and kCGWindowListOptionAll are causing XCode to report the compile-time error "Use of unresolved identifier [identifier]".
Somewhat surprisingly, though, if I right-click kCGWidowImageDefault and select "Jump to Definition", XCode does jump to the definition of that constant (in CoreGraphics > CGWindow.h > CGWindowImageOption) -- so XCode does seem to know what that constant is.
Here are the relevant snippets of my ViewController.swift file:
import Cocoa
import CoreGraphics
class ViewController: NSViewController {
...
func myFunction() {
// *** XCode reports the error on kCGWindowImageDefault on this line:
let imageOptions : CGWindowImageOption = kCGWindowImageDefault
...
}
}
The Apple documentation (as linked above) doesn't indicate what needs to be imported for these constants to be used.
XCode does appear to successfully recognize the types I'm using such as CGWindowImageOption -- it's just the constants that it isn't recognizing.
What do I need to do in order to successfully be able to use kCGWindowImageDefault and similar constants in my Swift MacOS program?
kCGWindowImageDefault is only for Objective-C, not Swift. In Swift, CGWindowImageOption is an OptionSet. For the default you simply use an empty option set:
let imageOptions : CGWindowImageOption = []
For CGWindowListOption you can do:
let listOptions: CGWindowListOption = .optionAll
Be sure you look at the Swift reference documentation for these enumerations. You can't use the Objective-C values.

Why private is unaccessible to the extension?

Here is my ViewController.swift file:
class ViewController: UIViewController {
private var myInt = 10
}
extension ViewController {
func printMyInt() {
print(myInt)
}
}
Although as mentioned in the Swift documentation - Access Levels section:
Private access restricts the use of an entity to the enclosing
declaration, and to extensions of that declaration that are in the
same file. Use private access to hide the implementation details of a
specific piece of functionality when those details are used only
within a single declaration.
Since Swift 4 has been released, I assume that I am able to implement such a thing (it is also mentioned in: What's New in Swift - WWDC 2017 session), however, the complier shows me:
'myInt' is inaccessible due to 'private' protection level
Is it incompatible with what mentioned in the documentation?
As a simple quick solution, I could declare it as:
fileprivate var myInt = 10
but I wonder why is it behaves like this, am I misunderstand what mentioned in the documentation? or is it a "Xcode" bug (used 9.0.1 version)?
Remark: The project has been created in the older Xcode 8 and then migrated to Xcode 9.
In Swift 4, private members are accessible to extensions of that declaration that are in the same file, see SE-0169 – Improve Interaction Between private Declarations and Extensions.
If the project has been created in Xcode 8 (with Swift 3) then
Xcode 9 will open it in "Swift 3 mode" and set the "Swift Language Version" to "Swift 3.2". Therefore the stricter Swift 3 restrictions hold.
To make the private extension visible to extension in the same file,
set the Swift language version to 4 in the build settings. (Of course
that might make more changes in your code necessary.)
Even better, use "Edit -> Convert -> To Current Swift Syntax ..."

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

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