Archive for the ‘How To’ Category

OS X Maps hint of the day

Want to grab the latest Maps data and don’t want to wait for updates? I’ve heard of a quick and easy solution, which is to delete ~/Library/Caches/GeoServices. This forces a cache flush and updates all your maps.

I noticed this might be especially handy today for those of you who use public transit in DC, Philly, Mexico City or Chicago.  Happy traveling all.

The ridiculous difficulty of getting aliased folders to update icons

psychedelic-volkswagen-1-927767-m

think I’ve got it down but not in a form that I could recommend anyone else using. Ever. Here are the steps that seem to work after massive googlage.

1. Update original (non-alias) icon as desired.

2. Delete aliases (Yes doctor, kill the patient)

3. Trash cache.

sudo /usr/bin/find /private/var/folders/ -name com.apple.dock.iconcache -exec rm {} \;
sudo /usr/bin/find /private/var/folders/ -name com.apple.iconservices -exec rm -rf {} \;
cd /Library/Caches/; sudo rm -rf com.apple.iconservices.store

4. Log out and back in or reboot computer. (No, really.)

5. Recreate the aliases or symbolic links.

For anyone who wants a good laugh, search the web for OS X alias icons won’t update.

Swift: Hacking APIs

tsmc001

Last evening a colleague asked whether it was possible to test private APIs in Swift playgrounds. Answer? Sort of.

Swift is not, as you will probably be unshocked to learn, not particularly friendly to API hacking. Playgrounds make it worth the bother by offering a perfect fit for exploration, testing, and prototyping.

Here’s what you do.

Read On…

Swift: Today’s Wow moment. Adding menus to playgrounds

I finally figured out how to get a nib/xib loaded into a playground and use it to power an app menu. It’s ridiculous how long this took and how simple the solution turned out to be.

Each playground, regardless of whether you’re building iOS or OS X code, runs as a tiny independent app on your computer. You can find that executable, and the app it is running in by peeking at NSBundle.mainBundle().executablePath.

Playground apps are tiny.  OS X versions consist of a minimal bundle with a stripped-down Info.plist, a Resources folder, and a MacOS executable.

You can throw a xib file into your Resources, but the playground cannot read it or run it because it’s not in compiled form. Today, I (finallly!) thought of using ibtool to pre-compile my MainMenu.xib file into nib and then load that. When you install Xcode’s command line tools, ibtool gets added to /usr/bin. So all you need to do to compile your nib is issue the following command:

ibtool --compile MainMenu.nib MainMenu.xib

Throw that resulting nib into your playground’s resources folder and you’re ready to load it up.

The trickiest bit was trying to get all the unsafe autoreleasing mutable pointer stuff working. Swift is like a perfect haiku. It all looks simple and obvious when you’ve finished writing it but getting it debugged is a pain. The answer, by the way is this, which hopefully people will be able to google up without having to go through an hour or two of tweakage:

var array = NSArray()
var outputValue = AutoreleasingUnsafeMutablePointer<NSArray?>(&array)
NSBundle.mainBundle().loadNibNamed("MainMenu", owner: nil, topLevelObjects: outputValue)

A few further points:

I built a simple compile command line shell script so I don’t have to remember the order of the files or the arguments for ibtool. You can see it in the video in the Resources folder.

Add your xib source into your Resources as well as your compiled nib. You can then edit the xib using Xcode editors directly from the playground workspace. You still have to compile outside it. I keep a terminal window open to simplify the process.

Every now and then Xcode automatically moves things around, so use a full path in the terminal. Then use the terminal’s history to re-issue the same command as needed:

cd /Users/ericasadun/Desktop/Playgrounds/X\ -\ Playgrounds/OSX\ Menu.playground/Resources ; ./compile

Don’t compile when your nib is in use.  Comment out the XCP indefinite execution line and the loadNibNamed line, and then compile in place from the command line.

You can’t much in the way of binding in your xib but you can design stuff there and then add the targets and behavior in the playground.

Swift: Fetching the updated docs as an ebook

Over at DevForums, users post that the 1.2 docs are online but not accessible in an ebook version.

Visit the prerelease website for the Swift Programming Language and you’ll see that the last update is 3/11/15 and the document revision history goes up to 2/9/15. So how do you get this website into an ebook form suitable for your iPad?

This is what I did.

1. Download Used sitesucker to fetch https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/index.html

Make sure the Download Option is set to Get All Files in Same Directory so you don’t inadvertently download all of developer.apple.com.

2. Locate index.html. Navigate into your Downloads folder into developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language. Find the Index.html folder

3. Launch Calibre. Grab a copy if you don’t already have one.

4. Drag index.html into Calibre. Wait for a while. Reading the metadata and adding to library takes time.

5. Select The Swift Programming Language.  Click Convert books. Choose EPUB or Mobi or whatever from the Output format popup. Set the Author to Apple.

6. Optionally add a cover image. If you want one from your existing Swift Language reference, navigate to ~/Library/Containers/com.apple.BKAgentService/Data/Documents/iBooks/Books and find the epub by grepping. Within the epub folder is a file called iTunesArtwork. Copy it to your desktop and add a jpeg extension then drag it into the Book Cover well.

7. Click OK. Wait for the book to convert.

8. Retrieve the new epub. Click Path: Click to Open. Your ebook appears in the newly opened folder. It’s big. Many many pages.

Swift: My favorite Playground feature of the week

As I code and test, I like to keep ongoing notes in text format. Normally, I add these to my workspace and remove them from build phases to ensure they don’t ship with my app. With playgrounds, I just discovered an entirely new approach and thought I’d share that with you.

In the File Navigator, select the Resources folder and type Command-N (File > New > File). Xcode automatically creates a new text file for you, places it into the Resources folder and opens a text field for you to edit its name.

Screen Shot 2015-03-19 at 11.48.41 AM

The notes file is bundled into the playground, so it travels with it, keeping my journal in-place for easy reference.

As I just discovered this, I didn’t get a chance to put this tip into the playgrounds book. I’m hoping once I figure out how to revise the book, it will be in the next version.

 

And about that…

People are mostly buying not borrowing the book and I’m timid about putting out a completely new version knowing they’d have to re-purchase to get the update.

What do you think about just emailing me when (if?) I put out a second version if you’ve already bought the first? There aren’t that many people who have bought this thing so far, so it wouldn’t be too much of a hassle.

I’d also love to hear what people have to say about switching over to iBooks instead. The KDP exclusivity clause prevents this with the Playgrounds book for 90 days but I could for any other titles. And iBooks does have auto-update.

Of course, given the sales numbers, it might not be a brilliant idea to try to go it alone for this kind of niche book except for a pure passion project like the Playgrounds book.

What do you think?