Whoa, Swift!

The biggest news that Apple dropped at WWDC 2014 this year is the introduction of the Swift programming language[1].

It’s going to take a while to sort out the syntax and get used to writing in an entirely new language. I’m not quite sure what to think of the change. I’ve been thinking and writing in Objective-C for so long that it’s very comfortable. I don’t despise its square brackets message-passing notation, the way some developers who are new to the platforms do.

I’ve been learning Haskell for a few weeks and writing about it; and I’ll probably do the same for Swift as time permits. It’s hard not to just dive in, though. So hard, in fact, that I rewrote a little admin tool that I previously wrote in Objective-C. I was curious about how the interaction between Cocoa and Swift would go. The tool just scans the Downloads and Desktop directories for any “qfx” files[2] and deletes them.

Here’s the ObjC version:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#import <Foundation/Foundation.h>

int main(int argc, const char * argv[])
{
@autoreleasepool {

void (^processDirectory)(NSString *,NSFileManager *) = ^(NSString *directory,NSFileManager *fm){
NSDirectoryEnumerator *enumerator = [fm enumeratorAtPath:directory];
NSString *path;
while( path = [enumerator nextObject] ) {
if( [[path pathExtension] isEqualToString:@"qfx"] ) {
NSError *deleteError = nil;
if( ![fm removeItemAtPath:[directory stringByAppendingPathComponent:path] error:&deleteError] )
NSLog(@"ERROR | unable to remove item at path %@. %@",path, deleteError);
}
}
};

NSFileManager *fm = [[NSFileManager alloc] init];
[@[@"Desktop",@"Downloads"] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSString *directory = [NSHomeDirectory() stringByAppendingPathComponent:obj];
processDirectory(directory,fm);
}];

}
return 0;
}

and the Swift version:

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

func processDirectory(directory:String, fm:NSFileManager) {
let contents:NSArray = fm.contentsOfDirectoryAtPath(directory, error: nil)
let predicate:NSPredicate = NSPredicate(format: "pathExtension == 'qfx'")
let qfxFiles = contents.filteredArrayUsingPredicate(predicate)
for object : AnyObject in qfxFiles {
let file = object as NSString
var error: NSError?
if !fm.removeItemAtPath(directory.stringByAppendingPathComponent(file), error: &error) {
println("ERROR | while deleting file: \(error)")
}
}
}

// main
let fm : NSFileManager = NSFileManager()
let directories = ["Desktop","Downloads"]
for dir in directories {
let path : String = NSHomeDirectory().stringByAppendingPathComponent(dir)
processDirectory(path, fm)
}

  1. Apple Computer. Swift Programming Language. https://developer.apple.com/swift/ Link

  2. qfx files are Quicken files. When I download transactions from financial institutions, these are the files that get deposited everywhere. I don’t like having them sitting around; so I wrote this admin tool to weed them out periodically. It’s set to run as a cron job launch agent.