Saturday, June 21, 2014

Gotchas for people coming from JVM languages

Initializers:


The signature includes the parameter names, so you can have two initializers, both taking the same types of parameters, as long as they have different names. eg.

init(withDouble: Double) {...}
init(withAnotherDouble: Double) {...}

And that's just fine.


Structs


 Structs are *always* copied, so if you have a struct Foo with a property Bar:

struct Foo{var bar = 10}

let foo = Foo(bar: 100)
var foo2 = foo

then modify foo2:

foo2.bar = 99
println(foo.bar + " " + foo2.bar) 

you get

100 99



Not really a gotcha, but if you don't declare your own initializers for a struct, and you have default values for all properties, then you get both an empty initializer

Foo() // bar will be 10

and a memberwise one

Foo(bar:100) // bar will be 100 

Function Parameter names are not external by default


func aFunc(param: Int){...}

Cannot be called like this

aFunc(param:10)

You need to explicitly set an external name for the parameter, or use #

func aFunc(externalName param:Int){...}

or

func aFunc(#param:Int){...}

or call it without parameter names:

aFunc(10)

Class Initializers 

Can be designated or convenience

Designated (init(...){...)})means that by the end of them, all properties will be set. Convenience(convenience init(...){...}) means that it'll only do a partial job. 

Have rules about what can call what

Designated initializers *must* call a designated initializer from their super class
Convenience initializers *must* call another initializer in the same class
Convenience initializer *must* ultimately call a designated initializer.

Have rules about the order you do things

Designated initializers must first initialize properties in the current class, then call a designated initializer in the parent class, then they can change their inherited properties.

Convenience initializers must call other initializers first, before they assign any values to anything.

You cannot access self, read properties, or call any methods until all the properties have been initialized - ie. once the calls to other initializers have returned.

Can be inherited


If you have defaults for all your properties, and you don't declare any designated initializers yourself, then a sub class inherits all the parent's designated initializers.

If you have implementations - either automatically from above, or by creating them yourself - of all your parent's designated initializers, then you inherit all their convenience initializers too.

Is and As

is checks if something is an instance similar to instanceof/isInstanceOf[T])

as/as? performs a cast similar to (Float) or asInstanceOf[T] note that you can do this to a whole array in one go, you don't have to cast the individual elements:

let bar: AnyObject[] = ....

for foo in bar as SomeClass[] {println("SomeClass: '\(SomeClass.someProperty)'")}

These checks only work with protocols if you annotate that protocol with @objc which will have the side-effect that that protocol can only be applied to classes.

Any/AnyObject

AnyObject is any instance of a class
Any is an instance of any type at all (except functions)


Monday, June 16, 2014

Updating iPad to iOS 8

Now that I've got the Swift development environment installed, it's time to upgrade an iPad so I can actually run this stuff.

go here https://developer.apple.com/devcenter/ios/index.action#

Click on iOS Beta, scroll down and find the link for your device (iPad 2 in my case - 1.4GB download) which for some reason didn't want to come down on Chrome, so I switched to Safari instead.

While that's downloading, I assumed I needed to register my device, clicked through various links, combed the organizer in XCode for an elusive 'Use for Development' link, then finally discovered that it doesn't exist anymore. Just manually register in the Dev Center and you'll be able to deploy straight to the iPad (once it's upgraded)

To upgrade the iPad plug it into your laptop and open iTunes (if it doesn't automatically). Click on the summary view (in my case, just click on the ipad's name on the nav bar) back up to somewhere (or don't, if you're cavalier about these things), then Option+click on check for updates (obscure..), select the ipsw file and off it goes.

If you get a 'firmware not compatible' error then the chances are you've downloaded the wrong ipsw - double-check you've picked the right model when you chose the iOS 8 download.


Right. Now to actually write something in Swift!





Tuesday, June 10, 2014

Swift - for a Scala Developer

Reading through the Swift programming language book was pretty familiar for a Scala developer - loads of the stuff I'm used to using every day is there: Options, Type inference, Types (including bounds), closures etc.

I have to say, that some of it is a bit clunky though (I realise this is the first version of Swift, so these things will likely improve, and that there might be some short cuts I haven't discovered yet)

For instance:

if let unwrapped = someOption {println(unwrapped}

vs.

someOption map println

OK, so that's super-compressed scala

someOption.map((unwrapped)=> println(unwrapped))

is a better direct translation

I think the reason this syntax particularly jarrs is that further on in the book it's specifically mentions that assignment operations don't return anything, so you can't mistakenly use them when you meant equality - yet here, let is returning a boolean.

I haven't started coding properly with Swift yet (or even made it to the end of the book), but already it's very familiar, with the playground being an improvement on the Scala REPL. I'm really looking forward to writing my first iOS apps.

Wednesday, June 4, 2014

Getting started with Swift

First - go to the Swift launch page, where it tells you to download Xcode 6 beta. You have to sign up to the developer program in order to get it (it's under iOS 8 beta).

Once you've signed up (and paid) you'll have to wait a little while for your acceptance to go through. For some reason I didn't get an email when it did, but it was under two hours so I just started reading while I waited.

As soon as your acceptance is through you can download Xcode - it's big 2.5Gig so you'll have time to at least read through the quick tutorial, then run the install which will take another age (the app finally ends up weighing in at over 5 gig)

I'm going to work through the book chapter by chapter, and post my thoughts and experiences as I do.