Archive for March, 2015

Swift. Slowly. A new short Kindle book.

Swift Slowly

In traditional terms, Swift is basically an unpublishable technology. It may have reached 1.2 but it’s still kind of a beta release (or, if you ask a few of my colleagues, alpha). It’s on its way to a generally usable language but it’s not quite there there yet. Swift provides no guarantees about source compatibility over time because it continues to evolve and improve both syntax and features.

So, I’ve decided to embark on an adventure. Last night, my new ebook “Playground Secrets and Power Tips” went live. It’s part of a planned “Swift. Slowly.” series. (I’ll get around to writing “A Swift Kick in the Apps” once the language stops jumping around.)

I’ve enrolled the book in Kindle Select, which means two things. 1. It’s not available on iBooks because of the Select exclusivity clause. 2. If you have prime membership or belong to Kindle Unlimited, you can borrow a copy for free. (The prime lending library must be accessed from a Kindle-branded device.)

If you don’t belong to Prime or want to read it on an iPad, you can outright buy the book for  four bucks, which after cuts and fees comes out to just enough to “Treat Your Author to Starbucks” money. I’ve requested DRM-free distribution so you can throw the file into calibre, convert it to epub, and read it in iBooks.

With Amazon, there’s no access to Apple’s book update program, which would have made my life much easier.  But on Apple, there’s no access to the lending program, which allows payment by the borrow. It’s an experiment. If it works, I have a long list of other topics I’d like to write about.

As for the book itself? I think you’ll find it to be a lot of fun. Playgrounds provide instant gratification for every line of code you type. Apple has put a lot of work into making these environments a fantastic touch point for language development support. My book discusses playground techniques both plain and advanced that will boost your development power.

Update 4/7/15: I have pulled the Kindle version of the book. It cannot be updated and its content is superseded by the iBooks version.

Xcode 6.3: Wheeeee! Beta threeeeee!

As far as I’m concerned, the biggest changes here have to do with playgrounds, which I’ll discuss shortly. However, there’s one new language feature that’s gotten me all happy and excited. flatMap (which I keep calling “flapMat” — perhaps it will catch on), will map a function over Arrays ( ArraySlice/ContiguousArray) and optionals and return flattened results.

With normal map, you apply a closure across each element.

var x = [["a", "b", "c"], ["d", "e"], ["f", "g", "h"]]
println(x.map{count($0)}) // returns [3, 2, 3]

But flat map goes an extra step. It applies map and then flatten. In its most simple state, you can de-array elements inside.

println(x.flatMap {$0}) // returns [a, b, c, d, e, f, g, h]

Or you can tweak the items a little, etc.

println(x.flatMap {$0 + ["XX"]}) // returns [a, b, c, XX, d, e, XX, f, g, h, XX]

Lily Ballard tells me this is also great for “When you have an optional and want to run an expression that returns an optional and flatten it, just like the ?. chaining operator, but the expression isn’t one that can be chained“. Here’s an example:

var optionalArray: [Int?] = [5, nil, 3, nil, 4, 6, 7, nil, nil, 8]
println(optionalArray.flatMap {(x : Int?) -> [Int] in
    return x == nil ? [] : [x!]
}) // returns [5, 3, 4, 6, 7, 8]

Playgrounds

Today’s other big changes have to do with Playgrounds. The file navigator now automatically includes two folders, one for Resources (you don’t have to edit bundles or use the “go to folder” arrow in the File inspector), the other for additional sources, so  you can declutter your playground and focus on getting things done.

Screen Shot 2015-03-12 at 12.00.44 PM

 

It took me all of about 2 seconds to add a new BezierPolygon.swift file so I could add custom QuickLook previews to a Bezier shape class. (Click the following to see the full-size screen shot).

Screen Shot 2015-03-12 at 12.03.53 PMYou do have to treat each file as if it’s part of a module. Make sure to use public liberally to expose elements to the playground.

Other quick items:

  • You can now upgrade playgrounds so you can use rich text formatting without having to create new documents and then copy/paste. Use Editor > Upgrade Playground
  • Rich text markup using standard Apple markup is no longer as crappy as it was in the previous beta.”Using the “/*: */ multi-line variant of Playgrounds rich comment markers no longer results in the comment block being duplicated further down in the document. (19917362)”
  • The playground is overall not as crashy crashy as in beta 2 due to stability enhancements.

 

Donner. Blitzen. USB-C

I own 3 lightning devices: an iPod touch, an iPad, and an iPhone. All day long, they’ve been stealing glances at me with sad little deprecation looks. The iPhone isn’t even 6 months old yet.

At least my spending on lightning cables has been pretty low compared to how much I accumulated in old-style docks and connectors.

Long live our USB-C overlords. Adapters galore are shipping soon.

The new MacBook: netbook? air? what?

The newly introduced 12-inch MacBook confuses me. Yes, I want one. Yes, I see where they will sell a lot of these to college students and on-the-go professionals. It has a great potential market. No, where I’m confused, is where this new MacBook fits in the product line. Is it the Air killer?

I was looking at a side-by-side spec-off this morning and noticing that while the display, battery life, and storage are up compared to a 13-inch MBA, the processor speeds are way down even with Turbo Boost. When Apple said it was taking lessons from mobile, it wasn’t kidding. The new MacBook takes the mobile crown from the MBA, complete with most of the touch points the Air had held.

In other words, what is the point of the MacBook Air anymore? Should they rename it the “MacBook Budget Alternative” instead?

The MacBook is more stylish, lighter, cleaner, with a better keyboard and a longer battery life. Its display kills the Air’s display. The processor downgrade is probably even a feature if you consider a lower load on the battery.

In other words, I’m beginning to think that the new MacBook exists to make anyone who buys the 2015 MBA feel very sad that they didn’t have the $300 or $400 to get the cuter model.

Utility: Finding untagged files and folders at the command-line

How do you find untagged items? This question popped up in #macdev for someone working on a Dropbox-based backup system of all things. So how do you find just those items in a folder that are untagged? Use mdfind:

mdfind -onlyin . "kMDItemUserTags != '*'" | open -f

If you switch that inequality to ==, you’ll retrieve all tagged items instead.

Folderol makes extensive use of tags. You can search for specific tags using names within the single parens, e.g. to find all the Green system tags:

mdfind -onlyin . "kMDItemUserTags == 'Green'" | open -f

The built in tag names are Red, Orange, Yellow, Green, Blue, Purple, and Gray, but you can assign any tag name you like in Get Info or by using the tag field in Folderol. Then, with a liberal use of asterisks, you can search for, for example, items with tags that contain the word Neon or neon (the [c] means case insensitive).

mdfind -onlyin . "kMDItemUserTags ==[c] '*Neon*'" | open -f

Or tags that start with Neon

mdfind -onlyin . "kMDItemUserTags == 'Neon*'" | open -f

Or end with Neon

mdfind -onlyin . "kMDItemUserTags == 'Neon*'" | open -f

In addition to the [c], you can use [d] (or [cd]) to add diacritical insensitivity, so a search for “café” would also match “cafe”.

To find all Folderol-set custom color tags, search for “(*)”. This excludes standard system tags, although if you’ve tagged with both a custom tag and a system one, the file or folder will appear in the results.

mdfind -onlyin . "kMDItemUserTags == '*(\*)*'" | open -f

Swift: Playground documentation of doom

XcodeScreenSnapz001

Xcode 6.3 beta 2 introduced one of my favorite new features: rich text documentation. This replaces all the playground building stuff you’ve seen around, both my extremely meh attempts and far better implementations like Jason Sandmeyer’s markup-powered generator, which remains available as an open source github repository.

Instead of constructing manifests and HTML docs, you just add comments using the new //: and /*: */ delimiters. There are a bunch of custom markup items detailed over at Apple but they work very badly in the current beta. Simple HTML at least for the moment provides a far better experience.

Stephen Celis (aka “the one who isn’t Sebastian”) continues to hold the prize for best/worst use of new in-playground documentation using the new system and HTML/CSS source. Of course, I’ve been trying for a while to reproduce his awesome works, but was stymied by the comic-sans. Using font-family css just wasn’t working for me.

Today, I finally cracked the case. Instead of font-family, I needed to use a direct font reference instead. What follows are my take on Celis’ three innovative technologies: Comic Sans, Blinky text, and Rainbow colors, all courtesy of intensive Google searches.

/*:
<head><style>
h1 {
color: gray;
font: italic bold 40px Comic Sans MS, sanserif;
}
p {
color: green;
}
.rainbow {
background-image: -webkit-gradient( linear, left top, right top, color-stop(0, #f22), color-stop(0.15, #f2f), color-stop(0.3, #22f), color-stop(0.45, #2ff), color-stop(0.6, #2f2),color-stop(0.75, #2f2), color-stop(0.9, #ff2), color-stop(1, #f22) );
background-image: gradient( linear, left top, right top, color-stop(0, #f22), color-stop(0.15, #f2f), color-stop(0.3, #22f), color-stop(0.45, #2ff), color-stop(0.6, #2f2),color-stop(0.75, #2f2), color-stop(0.9, #ff2), color-stop(1, #f22) );
color:transparent;
-webkit-background-clip: text;
background-clip: text;
}
@keyframes blink {
0% { color: red; }
100% { color: black; }
}
@-webkit-keyframes blink {
0% { color: red; }
100% { color: black; }
}
.blink {
-webkit-animation: blink 0.5s linear infinite;
-moz-animation: blink 0.5s linear infinite;
animation: blink 0.5s linear infinite;
}
</style></head>
<h1>Comic Sans</h1>
<p><span class="rainbow">Double rainbow across the sky</span></p>
<p>This is <span class="blink">blinking</span> text</p>
*/

Flashback to Voice Control

Someone popped up today with a classic iPhone 3GS, looking for docs on the VoiceControl system. Sadly, Apple has long since expunged nearly everything VoiceControl related from their main site.

Screen Shot 2015-03-02 at 4.31.36 PM

 

Following some links, I hopped over to the Internet WayBack machine to find that not much has been saved. Although the main voice control page was there, few of the secondary pages, videos, or assets were archived. (You can still link to a couple of audio clips, and the second one on making calls still plays.)

I finally googled up the iPhone iOS 3.1 user guide, which contains about a page and a half of text. This material is still hosted, and I’d imagine this continuing for the foreseeable future.

It was an interesting reminder about the fleeting nature of information.