Archive for February 6, 2015

Friday, February 6, 2015

Building Swift Bindings to libgit2

Brian Gesiak:

The str parameter is of type UnsafePointer<Int8>, but thanks to Swift’s ability to interoperate with C, we can just pass this function a Swift String. Read the Apple blog article linked above for details.

[…]

Unfortunately, in Interacting with C Pointers in Swift, @develtima explains that we can’t get a usable CFunctionPointer to a function defined in Swift.

So instead, Gift defines Objective-C helper functions that take a Swift closure, and execute that closure from within a C callback function.

Locks, Thread Safety, and Swift

Mike Ash:

Swift requires variables to be initialized before they’re used. pthread_mutex_init doesn’t use the value of the variable passed in, it just overwrites it, but Swift doesn’t know that and so it produces an error. To satisfy the compiler, the variable needs to be initialized with something, but that’s harder than it looks.

[…]

Getting data out of the block is a bit less pleasant, unfortunately. While @synchronized lets you return from within, that doesn’t work with with. Instead, you have to use a var and assign to it within the block[…]

[…]

Swift has no language facilities for thread synchronization, but this deficiency is more than made up for the wealth of locking APIs available in Apple’s frameworks. GCD and dispatch_queue_t are still a masterpiece and the API works great in Swift.

There’s No iOS Backup Feature

John Gordon:

There’s no iOS backup feature.

Yeah, I hear your scoff, but iCloud Backup is not Backup. It’s a system clone. If you delete Contacts accidentally, you can’t readily restore Contacts of, say, 3 days before. When my sister accidentally deleted most of her contacts she had no way to restore them from iCloud.

If she’d been synchronizing with iTunes she could have used a remarkably complicated hack: Recovering iCloud contacts, calendars, and bookmarks from an iTunes backup of an iOS device. Honestly, Apple, that’s just embarrassing.

Awk in 20 Minutes

Fred T-H (via Hacker News):

Awk is a tiny programming language and a command line tool. It’s particularly appropriate for log parsing on servers, mostly because Awk will operate on files, usually structured in lines of human-readable text.

[…]

An Awk script is structured simply, as a sequence of patterns and actions[…]Every line of the document to scan will have to go through each of the patterns, one at a time.

[…]

Finally, the last kind of pattern is a bit hard to classify. It’s halfway between variables and special values, and they’re called Fields, which deserve a section of their own.