Xcode 7 moves Playgrounds Console Output and breaks print() and println()

Xcode 7 Playgrounds behave differently than Xcode 6 Playgrounds – the user interface has changed, the Console Output has moved, and print() is different.

Playgrounds have the ability to show in-line code output, but don't be confused as it's not formatted the same way for println() and print() statements. Playgrounds "Quick Looks" provide a formatted view of your data, but if you want to see what it looks like, you need the Console Output. This moved back down to the Debug Area in Xcode 7 (from the Assistant Editor in Xcode 6).

Breaking println() and print behavior in Swift 2

Xcode 7 makes breaking changes to the behavior of the println() and print() methods. In Swift 2 you will be using print() to display text into the Console (Debug Area).

print() will append a newline to your text, which breaks the previous behavior where it didn't append newlines. If you want to prevent newlines you need to use the new optional parameter and pass in false.

// Swift 1.2
// Prints all on same line
for var i = 0; i < 10; i++ {
    print(i) // no newlines
//    println(i) // appends newline
}

// Swift 2
// Prints on separate lines, must use optional parameter for no newlines
for var i = 0; i < 10; i++ {
    print(i)  // appends newline
//    print(i, appendNewline: false)
}

Xcode 6 Playground Console Output

In order to see your Console Output from a print() statement in Xcode 6 you had to open the Assistant Editor (Venn Diagram icon) with the keyboard shortcut "Alt/Option + Command + Enter"

If you were lucky you would see output (assuming no code errors) and then if you accidentally closed the Console Output you would have to type something in the editor for the Playground and press Enter to get it to recompile and redisplay the Console Output.

Xcode 6 Playgrounds:&nbsp;Press Alt/Option + Command + Enter to see the Console Output

Xcode 6 Playgrounds: Press Alt/Option + Command + Enter to see the Console Output

Xcode 7 Playground Console Output

Xcode 7 changes how the Console Output works and makes this process less error prone and more streamlined. The Console Output is no longer displayed on the Assistant Editor, instead its below the standard editor in the normal Debug Area.

Press "Shift + Command + Y" to hide/show the Debug Area with the new Console Output, or you can press the tiny arrow button in the bottom left corner of your Playground in Xcode 7.

Xcode 7 Playgrounds: press Shift + Command + Y to see Console Output

Xcode 7 Playgrounds: press Shift + Command + Y to see Console Output

The good news is that the changes to Xcode 7 make the Playgrounds a more friendly code environment. It's easier to get to the Console Output without the clunky behavior of the Assistant Editor, which makes Playgrounds feel more like first class citizens.

Free Online Swift App Course

Subscribe to the iPhone Newsletter and learn how to make your first iPhone app in Swift with my free online iPhone app course.

OptionSetType Swift 2 Error: Nil is not compatible with expected argument

If you've been using Swift 1.2 or earlier, or you're an experienced Objective-C developer you'll discover using some APIs is different. 

Apple has been revamping all the APIs in Swift + Objective-C to support the latest Swift 2 language.

Apple has added Generics, Error Handling, and the OptionSetType which will change a large portion of the APIs – especially anything that takes option/setting flags.

Code Massaging

Moving forward you're going to have to massage your existing code, or your old programming habits to work with the new APIs.

I highly recommend opening a Documentation window (Shift + Command + 0) with Swift, since the Quick Help fails to work when you have a syntax error – something that didn't happened in Objective-C.

OptionSetType

The animatWithDuration: API that works with the spring physics has changed it's options: parameter to the new OptionSetType.

var myFirstLabel: UILabel!

func tapped(gesture: UITapGestureRecognizer) {
    UIView.animateWithDuration(0.5, delay: 0.0, usingSpringWithDamping: 0.9, initialSpringVelocity: 0.0, options: nil, animations: {
        
        self.myFirstLabel.center = CGPoint(x: 100, y:40 + 200)
        
        }, completion: nil)
}

In Xcode 6 this code works with Swift 1.2 or lower, however if you run it in Xcode 7 + Swift 2 you'll get the following Error.

Swift 2 Errors

You might see either of these errors, depending on the version of Xcode. Error messages are continuing to evolve, some are even more helpful in diagnosing the problem.

ViewController.swift Nil is not compatible with expected argument type 'UIViewAnimationOptions'

ViewController.swift Cannot invoke 'animateWithDuration' with an argument list of type '(Double, delay: Double, usingSpringWithDamping: Double, initialSpringVelocity: Double, options: nil, animations: () -> _, completion: nil)'

Solution

The error is because the type for the options: parameter is wrong – you'll have to pass in an empty OptionSetType in order to compile.

nil should be replaced with []

Apple has changed options for many existing APIs, instead of being a NS_OPTIONS bitmask, in Swift 2.1+ they are treated as a Set in syntax and behavior. To pass no options (i.e. nil) you pass the empty set: [ ]

UIView.animateWithDuration(0.5, delay: 0.0, usingSpringWithDamping: 0.9, initialSpringVelocity: 0.0, options: [], animations: {
    
    self.myFirstLabel.center = CGPoint(x: 100, y:40 + 200)
    
    }, completion: nil)

Follow me

Have you seen any strange error messages?

Send a Tweet to @PaulSolt

Fix UIViewController supportedInterfaceOrientations() to use UIInterfaceOrientationMask and OptionSetType in Swift 2

There are many changes coming in Swift 2 that further unify the Swift language and provide better support and method signatures.

A welcome change is the OptionSetType in Swift 2, this makes it easier to work with settings that relied on bit masks (a complex topic of binary logic) to use a Set and it's common operations.

The automatic conversion utility didn't help fix this change, so I had to fiddle a few times until I figured it out.

You'll see an error for your supportedInterfaceOrientations() method because the method signature has changed again. It now returns a UIInterfaceOrientationMask instead of an Int. 

Error: Method does not override any method from its superclass

While it breaks code now, this change makes Swift easier to use moving forward. Changes to how Objective-C code looks using new macros, generic types, and the OptionSetType will improve how you write code in Swift.

// Swift 1.2
override func supportedInterfaceOrientations() -> Int {
    let orientation = Int(UIInterfaceOrientationMask.Portrait.rawValue | UIInterfaceOrientationMask.PortraitUpsideDown.rawValue)
    return Int(UIInterfaceOrientationMask.All.rawValue)
}

Becomes a bit more verbose and uses the array notation to create a OptionSetType. It seems you need to set the type, otherwise the statement gets interpreted as an Array type, which isn't what you want.

// Swift 2
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
    let orientation: UIInterfaceOrientationMask = [UIInterfaceOrientationMask.Portrait, UIInterfaceOrientationMask.PortraitUpsideDown]
    return orientation
}

This new change helps make Swift 2 code feel more at home, and a little less crazy than the hoops you had to jump through in Swift 1.2 and earlier.

Links