Swift array of US state abbreviations

If anyone else needs it, here's a list of U.S. states in a swift array -- including Guam (GU), Puerto Rico (PR), and the U.S. Virgin Islands (VI).

let statesUSA = ["AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DE", "FL", "GA", "GU", "HI", "ID", "IL", "IN", "IA", "KS", "KY", "LA", "ME", "MD", "MA", "MI", "MN", "MS", "MO", "MT", "NE", "NV", "NH", "NJ", "NM", "NY", "NC", "ND", "OH", "OK", "OR", "PA", "PR", "RI", "SC", "SD", "TN", "TX", "UT", "VT", "VA", "VI", "WA", "WV", "WI", "WY"]

And here's a Rails gem way of getting a similar list: https://github.com/loureirorg/city-state

Debugging Autolayout views

One of my favorite tricks for making it easier to see what is going on with various views and trying to fix autolayout constraints issues is to set a different background color for every UIView in Interface Builder. 

But sometimes, it gets pretty ugly and it's nice to see it like the user ultimately will. In that case, drop this in the UIViewController viewDidLoad to remove all the background color settings:

// Clear background colors from labels and buttons
for view in backgroundColoredViews {
view.backgroundColor = UIColor.clearColor()

Image processing in Swift

One of the more tricky things to get right in apps is downloading and processing images from a server. I just learned of this new Swift library that looks like it does some of those heavy lifting bits for us: https://github.com/gavinbunney/Toucan

It's especially cool that it makes circular masking quick and easy. I think this will be my go-to library for an upcoming freelance project.

Locate the iOS Simulator directory

Sometimes I need to find out where an app is physically located when run in the iOS simulator. Putting this in the appdelegate.swift file will output the location:

let appDir = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).last as! String

println("App directory = \(appDir)")

Logging the custom fonts in an iOS app

I almost always add custom fonts to an app project, as they are an integral part of my UI design and maintaining that aesthetic throughout the app is (to me) the sign of a quality app.

But...it's also usually difficult to determine if the font you added is actually available (after modifying the info.plist file). In code, we need to use the exact "official" name for iOS to be able to use it.

So here's a code snippet to log all the fonts available to the console. It helps me when trying to debug a custom font issue:

var fontFamilies:NSArray = UIFont.familyNames()

for var i = 0; i < fontFamilies.count; ++i
var fontFamily: NSString = fontFamilies.objectAtIndex(i) as! NSString;
var fontNames: NSArray = UIFont.fontNamesForFamilyName(fontFamilies.objectAtIndex(i) as! String)

Alternating more than 2 table row background colors in Swift

I recently wanted to do a table view for a contract iOS project where the colors scaled up from a light color to a dark color and then back down to the light color. Over about 6 different shades and then down, for a "rolling color" effect.

I found this nifty feature in Swift that lets me do a modulo operation on an array of hex values stored in an array and set the table row color according to the result.

First, I set up my array of colors, light to dark and then back to light:

let ROWCOLORS:[String] = ["#FFF8E1", "#FFECB3", "#FFE082", "#FFD54F", "#FFCA28", "#FFC107", "#FFB300", "#FFC107", "#FFCA28", "#FFD54F", "#FFE082", "#FFECB3", "#FFF8E1"]

Then, in my cellForRowAtIndexPath method, I can use this:

for rowcheck in 1..<11
if (indexPath.row % rowcheck == 0)
cell.backgroundColor = UIColor(rgba:ROWCOLORS[rowcheck+1])

Note the 1..<11 notation. This is a cool way to run through a range of numbers without resorting to a case statement or a bunch of if..thens.


Swift – still early times?

I just started building a brand new version of an app for a freelance client and converting some of the previous app's objective-c code into swift. Mostly core data logic and server sync code. I can't tell if it's because I'm working on a 100% swift project, or using a beta release of cocoapods with swift framework support, but Xcode is crashing on me all the time. What's even stranger is that it's the Xcode *editor* that crashes, but only "temporarily". Couple that with some really strange thunderbolt display issues with Yosemite, and I have to say that Apple's QA on their software is really lacking these days. Arggghh!!