Validating US Zip Codes (Swift 3)

Erica Millado
Yay It’s Erica
Published in
4 min readAug 24, 2017
This game is real.

Today I was working on one of my side projects that requires users to enter a zip code to find local restaurants. I thought about how I could validate their zip code entries to see if they are ACTUALLY real US zip codes.

A few fun facts about zip codes before we get into code:

  • ZIP stands for “zone improvement plan”
  • Saks Fifth Avenue’s shoe department has their own zip code: 10022
  • The standard 5-digit length was implemented in 1963 due to the increase in mail.

Here’s how I went about creating an iOS app that checks if any given 5-digit number is a valid US zip code:

Step 1: Get valid US zip code data (as CSV).

I used downloaded an Excel spreadsheet of data from United States Zip Codes. I selected the “Personal” account (i.e. free) since I would be using the data for blogging purposes.

After signing up for a free/personal account, I downloaded a zip_code_database.csv file (you can also download it as .xls if you want to).

Step 2: Convert the CSV data to JSON.

I used the site Convert CSV to convert the CSV into JSON.

On the site, to generate an output, I selected the “CSV to JSON Column Array” conversion type to download.

After clicking “Download Result” I received a file named convertcsv.json.

Step 3: Rename the JSON & drag in a XCODE project.

I renamed my file zipcodes.json.

Step 4: Write code to read the JSON zipcodes.

I created a file called ZipCodeStore.swift to hold this logic.

1 — I wrote a static function that will send out a completion closure with an array of Ints (which will be the valid zipcodes!)

2 — I unwrap the URL location of the json file within my project and identify it by resource name and extension type.

3 — I convert this file into data.

4 — I serialize this data into a JSON object.

5 — I cast the json into an appropriate type [String: [Int]].

6 — I access the “zip” key in my json to get my array of zipcodes.

7 — I set my completion with this zipArray.

Step 5: Build the UI.

In Storyboard, I dragged on a textfield, a button, and a label.

I made sure that the text for the textfield was entered using only a numeric keyboard.

I then made outlet and action connections from Storyboard to the ViewController.swift file.

8 — These are the outlets for the view elements mentioned above.

Step 6: Create a local array with zip code data.

9 — Create a zipArray of Ints that will hold the zip codes returned from the json.

10 — When I call the completion on the .readJson function, I inject zipArray with the returned zip codes array (zipcodes).

Step 7: Handle the logic for the tapped button.

11 — Unwrap the text of the textfield (which should have numbers only entered in by the user) and cast it as an Int.

12 — My isValid(:) function takes in the user-entered zipcode (Int) and checks to see if the zipArray contains this value. It it does, the responseLabel text is changed to “Valid zipcode!”, or else it changes to “Try another zipcode.”

Check it out here (excuse the sound of me watching Godfather 2 in the background):

You can find the repo for this project here.

--

--