Tag Archives: 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

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

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