Category Archives: Uncategorized

Apple’s 40th

I’m feeling more emotional today than I thought I would. Today is Apple’s 40th birthday and I sit here reflecting on what that means to me on the bus heading in to work.

I feel an immense sense of gratitude and appreciation for this company. This affection and admiration started when I was a teenager and has only continued since. Apple is a company that still fundamentally believes in their mission to make the world a better place. This has been shown to be even more true once I joined the company and am on the inside.

I used to be afraid of corporations leading the world and taking on a bigger role than they should. I still am, I suppose. But Apple has shown me that this fear can sometimes be relaxed. That some can have grander goals that coexist with the traditional ones of being a large business. Their work on the environment, their unwavering defense of civil liberties, and desire to mesh technology and art are noble goals that make me want to work even harder to help continue this company’s mission.

I’m proud to work at Apple and hope to make some small difference in the work we do here.

Here’s to the next 40 years.

Secret Santa Sort

I finally got around to working on Brent Simmons’s challenge (or, rather, his pointer to a challenge) for implementing a Secret Santa sort. His original post is here and my Gist implementation is here. I like this solution as it effectively treats an array as a circularly linked-list in order to find a valid Santa assignment. This is meant to be run from the command line with the command cat secret_santa.txt | swift santa_sort.swift on a file like this:

Let me know if you have any suggestions or spot any errors!

Working with UIScrollView and Autolayout

Over the last few days I’ve read many, many posts on how to work with UIScrollView and Autolayout. Everything (including Apple’s documentation) is nothing but a convoluted mess. This article aims to provide the definitive source on using UIScrollViews within the Autolayout world. You can check out the source code to this demo application here.

Note that this article does assume some prior familiarity with the Autolayout system.

Embedding a view’s content within a UIScrollView is Apple’s recommended way for adjusting a view to compensate for the onscreen keyboard. Traditionally, this adjustment has been done with the use of frames. The introduction of Autolayout meant abandoning most use of frames but the need still exists to move content around on the screen with a UIScrollView and Autolayout. Here, I describe the steps involved to use these two technologies together.

To begin, there’s two ways to do it (well, really only one correct way to do it) but I’ll only be documenting one of them. The first method relies on adding subviews directly to the UIScrollView with no Container View. This is a pain because you must rely on tricks with the superview to make sure things align properly. It’s not fun.

However, the proper way to do it is easy and makes sense — once you understand it, of course!. Let’s dive right in.

1) Begin by creating a new ViewController in your project and add a UIScrollView to it.

step_1_2048x1305

2) Now, create constraints by CTRL-dragging from the UIScrollView to the View. You’ll want to create 4 constraints (hold the SHIFT key to select multiple): Leading, Trailing, Top, and Bottom constraints. Important: hold the OPTION key while doing this to align the UIScrollView to the Container (not the Container Margin).

3) Add a UIView to the UIScrollView and set up the same constraints as before: Leading, Trailing, Top, and Bottom. Notice that these constraints will already default to the Container (not the Container Margin). It’s handy to rename this newly added UIView to Content View for easier referencing later.

4) Now we’re going to add a few temporary constraints to make Interface Builder happy and for ease of understanding on our end. Add a constraint to the Content View for width and height and check the property Remove at build time:

step_4

 

This Remove at build time property will allow Interface Builder to fully compute our layout but the app will remove the constraints at runtime as they will be generated dynamically.

5) Finally, we can get to adding some content to our views. Drag a UIImageView out onto the Content View and make it 250pts wide and 250pts high, 20pts from the top, and centered along the Content View’s X axis. Additionally, make sure to add a sample image to the project so we can see something onscreen:

step_5_6_2048x1305

 

6) With this now setup add a UILabel centered on the image and spaced 500pts below it. Now, very carefully add one of the most important constraints: from the label to the Content View’s bottom. When you do this you can either set the Constant value to 0 or to Standard, whichever makes the most sense for your app. Adding these constraints should look something like this:

step_7_2048x1281

 

7) Next, add an Outlet from the Content View to a property within the ViewController class that is backing the Storyboard. I’ve named my property contentView. Like so:

step_8_2048x1281

 

8) Finally, you’ll add the following code to the viewDidLoad method of the ViewController class. This code binds the Content View’s Leading and Trailing edges to the root UIView’s edges. This allows the UIScrollView to implicitly calculate it’s contentSize and render the view appropriately. Run the app and you should have a label below your image and scrolling working as expected!

Following these steps should result in a functioning UIScrollView with Autolayout. I won’t cover the steps involved to show and hide the keyboard but that’s all covered in the aforementioned article by Apple.

UIImage File Extension

The UIImage class is a bit tricky to use when instantiating images from the Assets Catalog in Xcode. The tricky part is specified in the imageNamed class method of the UIImage class (https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIImage_Class/Reference/Reference.html#jumpTo_19):

On iOS 4 and later, if the file is in PNG format, it is not necessary to specify the .PNG filename extension. Prior to iOS 4, you must specify the filename extension.

So, you may see sample code that does not specify the extension when creating new images. This shortcut is only valid for PNG files so all other extension types need to specify. What makes it tricky is that the class method will return nil if the image is not found and the UIImageView.image property will accept this nil without complaining making it seem like everything is working.

UIFont Families

For my own future reference, here’s a list of all available UIFont Family names in iOS 8:

Academy Engraved LET

Al Nile

American Typewriter

Apple Color Emoji

Apple SD Gothic Neo

Arial

Arial Hebrew

Arial Rounded MT Bold

Avenir

Avenir Next

Avenir Next Condensed

Bangla Sangam MN

Baskerville

Bodoni 72

Bodoni 72 Oldstyle

Bodoni 72 Smallcaps

Bodoni Ornaments

Bradley Hand

Chalkboard SE

Chalkduster

Cochin

Copperplate

Courier

Courier New

DIN Alternate

DIN Condensed

Damascus

Devanagari Sangam MN

Didot

Euphemia UCAS

Farah

Futura

Geeza Pro

Georgia

Gill Sans

Gujarati Sangam MN

Gurmukhi MN

Heiti SC

Heiti TC

Helvetica

Helvetica Neue

Hiragino Kaku Gothic ProN

Hiragino Mincho ProN

Hoefler Text

Iowan Old Style

Kailasa

Kannada Sangam MN

Khmer Sangam MN

Kohinoor Devanagari

Lao Sangam MN

Malayalam Sangam MN

Marion

Marker Felt

Menlo

Mishafi

Noteworthy

Optima

Oriya Sangam MN

Palatino

Papyrus

Party LET

Savoye LET

Sinhala Sangam MN

Snell Roundhand

Superclarendon

Symbol

Tamil Sangam MN

Telugu Sangam MN

Thonburi

Times New Roman

Trebuchet MS

Verdana

Zapf Dingbats

Zapfino

Javascript Library Includes

While this may be extremely obvious to most people I’ve seen enough questions on StackOverflow and the like to warrant a mention:

When you’re using a Javascript library that depends on JQuery (or any other library for that matter), include the JQuery library before all others

Now, most people who get stuck with this problem may be coming from an IDE background where we have fancy tools to help negotiate dependencies. Most of time time, the browser can be smart about how it does resource-gathering but in this case Javascript doesn’t work like that. You’ll get strange errors that seemingly have nothing to do with your code.

So, for reference, include your depended-upon libraries first:

Clients

What I want to say to clients: “Yo son, this wild-ass update is maddd faster n’ shit.”

What I have to say to clients: “Hi, this new version is now much faster.”


I think all application developers must feel this way at some point.

How To Use Amazon MWS To Download Unshipped Order Reports

After spending many hours trying to understand Amazon’s API for accessing reports, I’ve finally come up with a solution. Below is one way to download an unshipped orders report.

Hopefully this example will help others trying to do something similar. Please let me know if you have any questions and I’ll do my best to help.