Xcode 7 Playgrounds can't use latest iOS/OS X frameworks?

Trying to test out some iOS 9 / OSX 10.11 stuff in a playground, but an seeing errors that seem to indicate that I can't try out new stuff in a playground. Specifically, trying to test out some things from the latest nshipster.com article:


import UIKit
let formatter = NSNumberFormatter()
formatter.numberStyle = NSNumberFormatterStyle.OrdinalStyle

This gives me an error in the console:

Playground execution failed: /var/folders/tf/3k7qp8615ml80kjsshq70hnr0000gp/T/./lldb/3312/playground8.swift:6:48: error: 'OrdinalStyle' is only available on iOS 9.0 or newer
formatter.numberStyle = NSNumberFormatterStyle.OrdinalStyle
                                               ^
/var/folders/tf/3k7qp8615ml80kjsshq70hnr0000gp/T/./lldb/3312/playground8.swift:6:48: note: guard with version check
formatter.numberStyle = NSNumberFormatterStyle.OrdinalStyle


So basically XCode 7 is useless to try out the latest code? Seems like a big problem...

Answered by Vision Pro Engineer in 16813022

Apologies, it looks like `guard #available` in playgrounds isn't functioning properly in beta 2. Instead, try wrapping your code in an availability check. It's not pretty, but it'll work (as far as I can tell from my testing) until a fix is available.


if #available(iOS 9, *) {
   // iOS 9 stuff
}

This is a known issue in the current beta. You can put the following line of code at the top of your playground to silence the error:


guard #available(iOS 9, OS X 10.11, *) else { abort() }


Note that this line of code will produce a warning, which can be ignored.

Yeah, that's not working. Xcode is showing an error:


Statement cannot begin with a closure expression


pointing at the '{', with a fix-it saying:


Explicitly discard the result of the closure by assigning to '_'


Doing the fix-it (which doesn't make sense), only creates another, same error, pointing at the '{' with the same fix-it



Taking out the OS X 10.11 (since it's an iOS playground), just gives a warning basically saying the statement is ignored because it's always true.

Will I fixed the error - the statement should be:


guard #available(iOS 9, OSX 10.11, *) else { abort() }


(Note - no space in OSX)


However, it still shows the warning:


Unnecessary check for 'iOS'; minimum deployment target ensures guard will always be true


and the rest of the code tried to execute, giving the same Playground execute failed error as originallly shown.


Guess we have to wait for Beta 3 to play with new Swift goodness...

Accepted Answer

Apologies, it looks like `guard #available` in playgrounds isn't functioning properly in beta 2. Instead, try wrapping your code in an availability check. It's not pretty, but it'll work (as far as I can tell from my testing) until a fix is available.


if #available(iOS 9, *) {
   // iOS 9 stuff
}
Xcode 7 Playgrounds can't use latest iOS/OS X frameworks?
 
 
Q