Make App Pie

Training for Developers and Artists

WWDC2016: Swift 3.0 and the Playgrounds for iOS

Last week the WWDC2016 left all of us with a lot to think about. I want to talk about some of the high points. I say some, because that’s a lot of stuff to absorb, and I didn’t get through enough to talk reasonably about all of them. The SiriKit session and all the WatchOS and tvOS sessions I didn’t have time to see yet.For this, I’m going to focus on Swift 3.0, and Playgrounds for iPad.

Swift 3.0

I have never heard such an awkward silence as when Apple did their best to try to hide an extremely painful change for most developers: Swift 3.0 changes. In the What’s New in Swift session, it was clear they know this was going to be a problem, and did their best to explain. It’s still going to be a pain. I heard over Twitter one developer that tried compiling their Swift 2.x app on their new Xcode 8 had two hundred and thirty syntax errors– after automatic conversion. While there are hundreds of changes in Swift 3.0 here’s the ones most likely to give everyone a headache:

Deprecation of ++ and —

This was one of those we’ve been warned about by the latest versions of Xcode. If you want to add or subtract 1 from a value the now ancient C operators ++ and -- are no more in Swift. Instead you need to use += or -=. For example, if you have

var x = 0
x++

You need to use

var x = 0
x += 1

C-Style for loops

The classic C syntax loop

for var int = 0 ; int >= 10; int++{...}  

is gone. Now you must use some of the for-in style like

for int in 0..>10{...}

to do the same.

First Parameters

Current Swift syntax let you declare a function

func myFunction(x:Int,y:Int){...}

and call that function with

myFunction(3,y:5),

You now have to call that function with

myFunction(x:3,y:5)

There is some good news. Using the underscore like this

func myFunction(_ x:Int,y:Int){...}

will let you call the function correctly. Otherwise you need to change all your function calls to conform to the new standard. While the reason for this is to make converting between Objective-c and Swift easier, it’s going to be a pain for developers transitioning code between Swift2.x and Swift 3.x.

Swift API Style Guide

There’s more here than meets the eye. Swift 3.0 has a style guide of how Apple and everyone else names functions and properties. I seriously suggesting watching Session 403 Swift API Design Guidelines, and check out the resources there. The short of it is to make your code read like a sentence. Functions and methods should be written as verbs, and properties and arguments as nouns.

NS is Gone

Many classes with the NS prefix in Swift are no more. Not the class, but the NS. NSDate, NSDateFormatter or NSTimer in Swift 3.0 is just Date, DateFormatter and Timer. It was after announcing that you could hear a very awkward silence. Your code that looked like this:

let date = NSDate()
let dateFormatter = NSDateFormatter()

Now looks like this

let date = Date()
let dateFormatter = DateFormatter()

I admit it’s a lot cleaner than previously, but still will be a lot of changes.

 

Extensions

If you haven’t worked with extensions in swift, this may be a good time to start. Extensions power many of the new API’s. Extensions do exactly what they say: instead of subclassing a class you add on more properties and methods to an existing class.

These existing classes include all API classes, where they become the most useful. In another lesson I discuss subclassing UITabBarController with a property to share models between tabs for example. Instead of subclassing, you could use an extension

extension UITabBarController{
	var mySharedModel = SharedModel()
}

and never touch or subclass UITabBarController. Many of the new APIs work by building extensions to them .

If you have the chance do catch Session 404 Getting Started in Swift on video. Even for a beginner’s session I learned a few things I didn’t know and it will sharpen your Swift skills.

Swift Playgrounds for iOS

I’ve wanted this for a while and passionately. It took a lot of effort for everyone in the Starbucks I was watching the Keynote to not look at me like a complete fool screaming and dancing. I wanted to — I didn’t. Apple introduced Swift playgrounds for the iPad, and it is far, far more than I could ever imagine. If you combine MIT’s Scratch language with Cut the rope in a Swift Playground, you’d start getting close. Add in complete access to the iPad’s UI and devices and you’d be even closer.

The Swift playground is directly targeted at the Education market. At first glance, it’s just a kids toy. Like Scratch, you can drag and drop code into the playground using a series of icons. The demo at The WWDC keynote had a series of lessons using both 3D and 2D animation for learning Swift. That series of lessons plays like many puzzle games where you have a character, in this case named Byte, perform some collection task, in this case collecting gems. The environment is rendered beautifully in animated 3D.

2016-06-19_11-06-37

The later demo for the Platforms State of the Union went into writing code you can import directly into Xcode. By adding import UIKit to your playground, you have all the UIKit API’s available to your playground — and all the devices. The demo included creating a new control for a color picker.

The playground is included in the iOS 10 beta. I downloaded it for my iPad Pro. Once you open it, you can download the Learn to Code Playground. I at first thought this playground was going to be a lame very basic tutorial. It’s actually one of the most addicting puzzle games I’ve played in a while. I planned to try just one level (using commands) and ended up completing the functions and loops levels as well. The levels go through most of the basic language, leaving more advanced topics to a second set of activities which at the time of their writing have yet to be released.

In these lessons you are really programming, not just filling out forms. I added a few advanced features to one lesson just to see how it would react and it just worked just fine.

If you want to get serious, you can open a blank playground. By importing UIKit, you have all the functionality of an app in Xcode. If you want to quickly play around with a single view application, you can easily do that in a few lines of code.

>import UIKit
import PlaygroundSupport

class ViewController:UIViewController{
    override func viewDidLoad() {
        super.viewDidLoad()
        
    }
    
}

let viewController = ViewController()
PlaygroundPage.current.liveView = viewController
PlaygroundPage.current.needsIndefiniteExecution = true

This give you full access to a view controller. The PlaygroundSupport library had several classes which give you the playgrounds as a simulator. As you’ll se it the code above with only four lines of code. Of course, there’s no storyboard, so you’ll have to be more programmatic about your views. I wrote some quick code to make a table view that looked pretty good and completely interactive.

2016-06-19_11-05-54

Playgrounds support markdown in your code, so you can write full tutorials like Apple did. Using a special file structure, a few plists and some of your own code, you too can write stuff for playgrounds.

Overall I’m very impressed with Playgrounds. It’s a great mobile tool for trying out new code without the overhead of Xcode. I went into WWDC2016 the least optimistic I have ever been with one, I’m far more optimistic now. While there’s a lot of work in migration, the end result will be far cleaner, understandable applications in Swift 3.0.

One response to “WWDC2016: Swift 3.0 and the Playgrounds for iOS”

  1. This is a great post. Thank you for the info.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: