Process CSV Streams into Codable Objects

Doing a lot with CSV lately. I had started building a CSV parser in Swift several years ago when actively using an Objective C framework that I kept finding bugs in. That framework used pointers to extract fields, which really made things difficult to debug.

One objective is to allow incremental processing, as happens with web streams: in the past, I handled millions of records and it was obviously important to start showing users results as soon as a few records were decoded.

As a semi-retired dev (not my choice!), I decided to upgrade the Swift code and “do it right”. The project is table driven and uses a state machine. Unit tests probe many of the edge cases (e.g., empty file, just one column thus no delimiters, spaces before or after fields, etc).

It has taken a lot longer than I thought, and the unit tests took longer to write and debug than the original source! [Anyone telling you after weeks of development to write some unit tests in day is totally disconnected from reality—this happened to be recently too!)

What I’ve been doing is modifying all my open source that deals with streams of data, and make them comply to Apple’s Streams interface (InputStream and OutputStream). It turns out that with a Steam interface, its pretty easy to make them work with Combine, as Operators (combination of a Publisher and a Subscriber). The reason for being an Operator is that they accept a Publisher’s raw data stream (as a Subscriber) then output something (as a Publisher). In the case of CSV, the code takes a CSV line and decodes it into a struct or class that compiles with a simple protocol, CSVDecode. In practice, an app using it will direct a stream of data (file or network), and get a stream of objects in return.

In addition, the code is packaged as a Swift Package, recently supported in Xcode 11. Packages are great!

What really helped me was to create a diagram of how CSV works. There’s actually a RFC describing it, along with common extensions, all of which this code supports. A new program on the Mac App Store, Diagrams, was a huge help! It’s an easy to use and not expensive: it took me about 5 minutes to master it. For instance, this is what I created to let me code the comment line section:

Screen Shot 2020-03-13 at 5.35.46 PM

The overall state diagram took me a lot longer than I thought it would too! I kept finding edge cases.

Next, I’ll write a test app that lets people access a slew of CSV repositories on the web. If you want to see the source click this link SAX-CSV-Parser-Package.

Nice technique to specify log messages

Just changed jobs, and having to switch back to Objective C for most of my work. I’ve used a selectable macro in the past for log messages:

#if 0 // 0 == no debug, 1 == lots of messages
#define LOG(...) NSLog(__VA_ARGS__)
#define LOG(...)

You can add this to each source file, and selectively turn messages on or off with one tiny change. But the problem is, you look in console and there are slews of log messages. So how to know which is from what file?

Well, I just found out I can change the above to:

#if 0 // 0 == no debug, 1 == lots of mesages
#define LOG(...) NSLog(@"DBManager: " __VA_ARGS__)
#define LOG(...)

That is, since Objective C will coalesce strings, it will merge @”DBManager: ” into whatever format string you used.

I suppose the __FILE__ or NSStringFromClass([self class]) might work too – but I didn’t try.

How to Calibrate a New Monitor

The Apple ColorSync calibration screens are useless. I entered a bug over 8 years ago on it, and nothings changed.

My over-5yr-old Hanns-G 27.5″ model died (cost $400+) and I replaced it with a 27″ LQ 27MP57HT-P monitor (cost ~$125!). However, when I plugged it in to my Mac, it looked terrible!

After trying the Apple calibration, I re-discovered a web site that I now remember using to calibrate the old monitor: LCD Calibration

Using the sharpness test, I discovered that the LG essentially turns it off at a setting of 50, not 0 and not 100.

In the end, I found settings of:

Brightness: 70
Contrast: 71
Black Level: High
Super Resolution: Off
Color Levels: All at 50

Hope this helps someone someday. I found nothing googling around.

Incredibly small+compact+clever Swift code for descriptive Auto Layout constraints.

I book marked SwiftAutoLayout when I first saw it announced. This amazingly compact and innovative bit of code was written by a young Canadian student Indragie Karunaratne.

Currently, there are several popular DSLs for doing Auto Layout related work (mostly creating constraints), but they are all (IMHO) way to large and all encompassing. Indragie’s code is lightweight, clear, and does most of what you’d want. I often evaluate code I use with the question: “Can I maintain this if the original author abandons it?” Clearly this code falls into the “yes” category (for me and for most others too!)

This code hadn’t been updated since early this year, so I
forked it and addressed one feature request and added support for new constraint options available in iOS8.

Lets look at an example of creating two constraints:

let constraints: [NSLayoutConstraint] = [
contentView.al_centerX == rLabel.al_centerX,
detailLabel.al_baseline == rLabel.al_baseline
] // wrapped for easier viewing

In this simple example, there are two equalities. More complex constraints might have a multiplier, constant, and priority, in which case they’d look like:

let constraint = view1.al_left == view2.al_right * 2 + 10 ~ 100

instead of the standard method:

let constraint = NSLayoutConstraint(item: view1,
attribute: NSLayoutAttribute.Left,
relatedBy: NSLayoutRelation.Equal,
toItem: view2,
attribute: NSLayoutAttribute.Right,
multiplier: 2.0,
constant: 10.0) // wrapped for easier viewing

constraint.priority = 100

Almost better is the wow factor you get when reading the code to understand how it works! By using extensions to UIView (the al_… attributes), these expressions get turned into ALLayoutItem structures, which you the user never see. They simply act as placeholders for values, so the expression can evaluate to a single NSLayoutConstraint!

It took me a few reads of the code to “get” it, then “Wow!”. In the end, you get an incredibly useful utility barely consuming 250 lines of code, and have the opportunity to see Swift used in a way you probably never even thought of.

[Lastly, I should add that Indragie contacted me, and has asked for a pull request so he can wrap this Swift 2.0 code into his original repository – something I will do shortly when iOS 9 goes GA.]

Amazing technique to embed non-optional assignments within a comma separated “if let” statement

The problem is, you have this complex set of assignments to complete before you can take an action, and most of them are optionals – but not all:

for cell in tableView.visibleCells {
  if let indexPath = tableView.indexPathForCell(cell) {
    let user = users[indexPath.row]
    if let ratingLabel = cell.contentView.viewWithTag(100) as? UILabel { 
      setRatingLabel(ratingLabel, user: user)

Now imagine more assignments, a few optionals then a non-optional – it regresses to the old “cascading if let” situation in pre-Swift 1.2 code.

This type of situation appears in my code all the time, so I

posted a question on the Apple internal Swift forum asking for suggestions.

Fortunately, it was answered by OOper! It seems that you can embed a non-optional assignment by prefix it with “case”:

for cell in tableView.visibleCells {
if let indexPath = tableView.indexPathForCell(cell),
   case let user = users[indexPath.row],
   let ratingLabel = cell.contentView.viewWithTag(100) as? UILabel
     setRatingLabel(ratingLabel, user: user)

I’d love to say I completely understand why it works – I think it’s because ‘case’ statements can fail or succeed. In any case all my code using this works just fine.

While looking a bit odd, the final code is more compact and easier to follow.

Swift 2.0: Catching enums having associated values

Many blogs touch on the ability of Swift to throw an enumeration that contains a payload value, but no one has really shown how to get the associated values – particularly if the different enumeration payloads differ in type.

Here is an example, which shows how to catch those errors and extract the associated value:

enum Foo : ErrorType {
case First(String)
case Second(Int)

func flup(i: Int) throws -> Bool {
  if i == 0 {
    throw Foo.First("Howdie")
  if i == 1 {
    throw Foo.Second(2)
  if i == 2 {
    throw Foo.Second(4)
  return true


do {
  try flup(0)
} catch Foo.First(let error) {
  print("ERROR: \(error)")
} catch {
  print("WTF: \(error)")

do {
  try flup(1)
} catch Foo.First(let error) {
  print("ERROR 1: \(error)")
} catch Foo.Second(let error) {
  print("ERROR 2: \(error)")
} catch {
  print("WTF: \(error)")

do {
try flup(2)
} catch Foo.First(let error) {
  print("ERROR 1: \(error)")
} catch Foo.Second(let error) {
  print("ERROR 2: \(error)")
} catch {
  print("WTF: \(error)")

This is what appears on the console:

ERROR: Howdie
ERROR 2: 2
ERROR 2: 4

How to develop a common subclass for UIViewController, UITableViewController, and UICollectionView controller?

This task is seemingly impossible. You’d like to have some common methods to use for all three controllers. Because of the apparent lack of the ability to “interject” a UIViewController subclass into a UITableViewController, I just never used it. When UICollectionViewController came out, though, I was determined to find a way to do this.

At first I tried using a UIViewController – .m and .h – with macros, but I never got it to work properly. Finally, I created two files,  BaseClass.h and BaseClass.m. In the former, I just declared the methods themselves along with any public properties. Then in the implementation file, I coded the methods. Note there is no “@interface…” or “@implementation”.

Finally, I created three subclasses, STViewController, STTableViewController, and STCollectionViewController, and between the interface declaration and “@end”, included the base class file. Voila – works like a champ.

What’s even more amazing is that Xcode’s editor works just find with these naked methods when editing BaseClass.m – it parses them and highlights errors just as it would for a normal class!

A property/ivar/outlet was just changed, how do I find out who did it?

I see this post all the time on StackOverflow. The answer is surprisingly simple:

– if you have an ivar, convert it to a property with a synthesize ivar=ivar if needbe to avoid having to prepend a “_” to usages

– write your own setter, and add logic tests and NSLog messages

– put a breakpoint on the NSLog message, and run your app

Voila! You app stops when the value changes, and you can see who the offender is!

URL Verifier, Parser, and Scraper in Objective-C

I just updated the second version of my github project, URLFinderAndVerifier. Needing to verify a http URL as it was typed in to toggle an “Accept” button, I searched the web for such a thing, and unfortunately found oodles of them, none of which had any credentials.

Looking closer on another day, I found a site run by Jeff Roberson where he had meticulously worked through RFC-3896, constructing simple regular expressions then combining them to create a full regular expression that should properly parse any valid URL. Note that the standard allows much of what we think of as necessary information to be empty.

So I started with his expressions, then tweaked them slightly to handle more real world conditions, such as forcing a person to enter (the optional) ‘/’ at the start of the path segment (which acts as a end of authority marker).

Using my previous Email verifier test harness, I constructed a test engine that lets you test out various combinations of options. It has three components:

– construct the regular expressions for use in a text file or a NSString constant

– URLSearcher, a class that uses to regular expression to find or verify URLs

– a test app that you use to test various URLs, or “live” input mode


All regular expressions exist in text files that are heavily commented. A pre-processor reads these files and removes comments and extraneous characters (like spaces). This makes the files much more readable and understandable. These partial regular expressions that are used to build a full regular expression based on the options you select in the GUI.

Users can optionally enter Unicode characters, which the regular expression can optionally allow. Given a verified URL, a utility function in URLSearcher converts these syntactically correct strings to ‘%’ encoded ones that fully meet the RFC spec (Europeans should like this!)

Other options are to look for every scheme, or http/https, or http/https/ftp and the ability to spec capture groups to see URL subcomponents: user, host, port, path, query, and fragment.

PS: if you end up using this it would be great if you could give the StackOverflow post an up-arrow.

Tracking system object deallocs

Have you ever had a time when you had a memory leak, but was unsure if the problem were your objects, or that some collection item holding them wasn’t getting dealloced?

Well, now you don’t need to wonder any more:

This demo project highlights the Tracker class. After creating and initializing say a mutable array, ask Tracker to track it:

[Tracker trackerWithObject:obj msg:@”Some Msg”];

If obj does get dealloc’d, you’ll see a NSLog message in the console. Or, at some point where you think obj should have been dealloced, ask the class for the current list of tracked objects and dump their descriptions to the console.

You probably don’t need this functionality often, but when you need it, its invaluable!