Website Logo. Upload to /source/logo.png ; disable in /source/_includes/logo.html

Hacker School Log

Laura Skelton | Summer 2014 Batch

Hacker School Day 1: Swift Tricks

Things got much more tricky when I started trying to implement SimpleShare, my Bluetooth LE (CoreBluetooth) mesh network sharing app, in Swift, using the new Multipeer Connectivity framework instead of Bluetooth. There were much more complicated things I needed to do in Obj-C, and the app couldn’t be “translated” in the same way as the simpler apps just by changing to the new syntax, since Swift actually works differently in key ways than Obj-C does.

Things I learned about Swift:

  • Optional variables: You can no longer have variables that aren’t set or that are nil like you could in Obj-C. If you don’t know what a variable is when the class is created, you have to make it an optional variable by putting a ? after the type declaration.
1
2
3
4
class MyClass {

    var mystring: String?
}
  • Global variables: It seems that any variables you set within a class but outside of a function act like public parameters for that class and are accessible to read or set from other classes

  • Calling functions from another class: You call a function from another class by writing the class name followed by a dot then the function name with its arguments, eg: MyClass.someFunction(arg1,arg2)

  • Init functions are a special case: Instead of calling MyClass.init(arg1,arg2) like you would for any other function, just calling MyClass(arg1,arg2) is how you can init an instance of a class.

  • Convenience init: If your init function requires arguments to create a class instance, you can create a second init method for convenience that creates an instance with default values.

1
2
3
4
5
6
7
8
9
class SimpleMesh {
    init(simpleMeshAppID: String) {
        self.simpleMeshAppID = simpleMeshAppID
        self.myItemIDs = ["first", "second", "third", "fourth", "fifth"]
    }
    convenience init() {
        self.init(simpleMeshAppID: "[NO ID]")
    }
}

In the afternoon, I made a plan with Jorge and Kyle to work on a mesh networking inspector to figure out the limitations of Multipeer Connectivity so that we can build a stable mesh networking messaging app. We decided to move forward on the project in Objective-C while continuing to learn Swift on the side so we can finish more quickly.

Hacker School Day 0: Swift Playgrounds

First day of Hacker School! It was amazing to meet so many people who shared interests and had new ideas about programming.

I had planned to get to work on Machine Learning and getting comfortable with neural networks, but since the announcement of the new Swift language for iOS, there was a lot of interest in diving into learning that.

I learned about algebraic data structures from some other Hacker Schoolers over lunch, which was cool. I could see the recursive tree structure being a useful way to set up a decision tree in another language and will have to learn more about whether the data structures available in some other programming languages would make the machine learning algorithms possible to be coded in a more obvious, intuitive way than they are generally set up in Python.

I got together with a group in the afternoon to learn about Swift and the new Playgrounds Apple introduced in XCode 6. Playgrounds are cool because you can copy a snippet of code you’re working on and see how the variables are being set and the values are changing over time as it runs through a loop.

I paired with Denise on getting some Swift code working. We figured out the brand-new syntax for hooking up variables to Storyboard outlets so we could generate a simple app with a button and some labels that changed text. We learned that every variable in Swift is public, as opposed to Obj-C which lets you set only certain parameters to be accessible from other classes. Once we got things going, the syntax seems much simpler and faster than all of the boilerplate code you have to write to do the same things in Obj-C.

Later in the afternoon I worked on copying some simple demo apps I’d written previously in Obj-C into Swift so I could get more comfortable with the new syntax. Translating the very basic apps was relatively straightforward.

Coolest thing from Day 0: Emoji as variable and function names in Swift! Cutest language ever?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import UIKit

class MyViewController: UIViewController {

    @IBOutlet var ๐Ÿถ: UILabel
    var ๐Ÿ˜ป = "๐Ÿถ๐Ÿท๐Ÿน๐Ÿฐ๐Ÿฑ"

    @IBOutlet var ๐Ÿน: UIButton

    override func viewDidLoad() {
        super.viewDidLoad()

        ๐Ÿถ.text = ๐Ÿ˜ป

    }

    @IBAction func ๐Ÿญ() {
        ๐Ÿถ.text = "๐Ÿฐ๐Ÿฏ๐Ÿต๐Ÿฎ๐Ÿจ"
    }

}