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 Int
s as states, and a single Int
as input.
Tag Archives: Swift
UITableView in XCode 8.0 (beta) Playground
In this post, I’ll be (slowly) building a UITableView
application programmatically in a Playground. First we’ll add a simple UITableView, then we’ll add a UINavigationBar. In future edits, I’ll add some more views.
Negative Slicing and Indexing of Strings in Swift
I ran across this SO post regarding indexing and slicing Strings in Swift, and I added some negative indexing and slicing functionality to the extension.
Continue reading Negative Slicing and Indexing of Strings 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.
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.
Solving Linear Equations with Swift and Accelerate
In this post we’ll cover solving a system of linear equations using Swift and Accelerate. It get’s a little bit hairy, but it’s not so bad once you get the hang of it.
Continue reading Solving Linear Equations with Swift and Accelerate
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
Using NSOutlineView and NSTreeController to Create a Tree
In this post I’ll walk through setting up a hierarchical view with expanding nodes using NSOutlineView and NSTreeController. This is the sort of thing you’d want if you had to describe a file system. In this example I’ll be using Xcode 7 and Swift 2.
Continue reading Using NSOutlineView and NSTreeController to Create a Tree
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