Tag Archives: Swift

Deterministic Finite Automata in Swift

I’ve been reading Understanding Computation by Tom Suart, and it reminds me of my first CS class in grad school where I learned about and struggled to implement finite automata. I think one major lesson from that experience was to forget about the pictures of circles and loops on the blackboard, and figure out the bare minimum needed to represent the idea. In the case of finite automata, the bare minimum was that there was a state, and a set of rules that modified the state; each rule was composed of a current state, an input, and a next state. I decided to implement a simple finite automata in Swift using a list of Ints as states, and a single Int as input.

Continue reading Deterministic Finite Automata in Swift

Swift Optimization

I learned a hard lesson today: to make Swift really fast you have to know what you’re doing. You can’t just slap some code together and expect it to be zippy without understanding some of how Swift was designed and how it works. There is a very good presentation by some of the team members that built Swift here. My initial takeaway was that it was important to finalize any classes you didn’t plan on subclassing, incrementally check the timing analyzer, and finally, employ whole module optimization.

Continue reading Swift Optimization

Using MapKit in an XCode Playground

I saw some outdated material about using MapKit in an XCode Playground, so I thought I’d post an example for XCode 7.1.1 and Swift 2.1. Being able to access and manipulate a map this easily if freaking amazing. This code allows you to view the map in the timeline on the right-hand pane. You can press the diagonal Venn diagram at the top right-hand corner to toggle the timeline.

Continue reading Using MapKit in an XCode Playground

Matrix Inversion Using Swift and Accelerate

I found out how to invert a matrix on SO, but I didn’t understand the solution, so I thought I’d talk more about it here. First of all, there isn’t a one-off inverse function in the Accelerate framework. You need to calculate the LU facotrization first using dgetrf_(), and then plug that data into dgetri_() to calculate the inverse.

Continue reading Matrix Inversion Using Swift and Accelerate

Write to a File in Swift2

Swift2 came out yesterday! For free! I thought that Swift2 would have built-in support for I/O, but it looks like you need to import Cocoa first.

import Cocoa

let data = NSString( string:"Hallo, Welt!" )
let destPath = "/Users/connorjohnson/myFile.txt"
var filemgr = NSFileManager.defaultManager()
do {
    try data.writeToFile(destPath, atomically: true, encoding: NSUTF8StringEncoding)
} catch let error as NSError {
    print("Error: \(error)")

Working with Strings in Swift

In this post I’ll discuss replacing characters in a string, splitting a string using some value, and using regexes to count the number of matches in a string, and locating the first match in a string. I’m using XCode 6.4 with Swift 1.2. If you’re not sure which version of Swift you’re running, you can call the following from the command line,

$ xcrun swift --version
Apple Swift version 1.2 (swiftlang-602.0.53.1 clang-602.0.53)
Target: x86_64-apple-darwin15.0.0

Continue reading Working with Strings in Swift