I forgot how much I hate the fickleness of writing software!
I finally gave up on Swift (for now). It’s a easy to pick up language because it resembles more a modern scripting language than a compiled one, and frees you up from historical artifacts like Smalltalk, C pointers, and reference counting. Plus you get awesome things like playgrounds:
Sure, it takes a little bit of chucking random “?”’s and “!”’s in your code before you get the hang of optionals, and I’d fear the performance of a basic structure like a b-tree written in Swift, but it interoperates with C and Objective-C so I don’t have to worry.
Or so I thought. Because of strong typing and the lack of built-in overloads, poorly documented character-based1 functions, here is how I inject a a random letter into a string:
let randomletter = "\(String(UnicodeScalar(65 + Int(arc4random_uniform(26)))))"
And then you find it doesn’t always release variables when you use optionals. create some optional simple objects, add them to an array reset everything and see what your NSLog()
(e.g. println())
) prints in your deinit:
e.g. BNRItem.swift:
import Cocoa
import Foundation
class BNRItem: NSObject, Printable {
var itemName:String
init(itemName name:String)
{
self.itemName = name
super.init()
}
deinit {
println("Destroyed \(self)")
}
class func randomItem() -> BNRItem
{
let randomAdjectiveList = ["Fluffy", "Rusty", "Shiny"]
let randomNounList = ["Bear", "Spork", "Mac"]
let adjectiveIndex = Int(arc4random_uniform(UInt32(randomAdjectiveList.count))) //arc4random() overflows Swift int
let nounIndex = Int(arc4random_uniform(UInt32(randomNounList.count)))
let randomName = "\(randomAdjectiveList[adjectiveIndex]) \(randomNounList[nounIndex])"
return BNRItem(itemName: randomName);
}
override var description:String {
return "\(itemName)"
}
and main.swift:
import Foundation
var items = BNRItem[]()
var backpack:BNRItem? = BNRItem(itemName: "Backpack")
items.append(backpack!)
var calculator:BNRItem? = BNRItem(itemName: "Calculator")
items.append(calculator!)
backpack = nil
calculator = nil
items.append(BNRItem.randomItem())
items.append(BNRItem.randomItem())
for item in items {
println(item)
}
// Destroy the mutable array object
println("Setting items to nil…")
items = []
creates 4 items, deletes three. Hello, memory leaks!
Then there are issues when you call C functions like CoreGraphics from Swift. My personal favorite is depending on if I restart Xcode before compiling, one of my apps compiles file or throws an error:
SetAppThreadPriority: setpriority failed with error 45
Great!
…
That’s not to say I won’t use it for the future. It’s just too frustrating for me to programming model (Cocoa Touch) in a language that I’m rusty at (Objective-C) in a new IDE (Xcode), translate to a language I don’t know (Swift), and not know if my errors are my own stupidity, or just the compiler is buggy.
Yes, someday Swift, and Swift playgrounds for learning the language, and perhaps even a mix of Swift and Objective-C in a iOS project, but I’m putting the language down for now until I learn Cocoa Touch.