Swift 2 Unit Testing & UI Testing Resources

Starting to write unit tests in Swift 2 / Xcode 7 so thought I would keep a list of resources and share.  Plan to keep this post updated as I find more.  Feel free to ping additional resources in the comments!

 

Google Analytics & Swift

students of knowledge working on Apple hardware

I’m working on an iOS app written entirely in Swift that uses the Google Analytics library.  At Google I/O 2015 they announced full support for Swift and even CocoaPods (which doesn’t actually work if any of your other libraries use_frameworks! – but they are working on that and it’s not a trivial fix).  The initial intro documentation from Google has options to show the code sample in either Objective-C or Swift, but all the rest of the documentation still seems to be Objective-C, only.  I was listening to the More Than Just Code podcast tonight and Aaron Vegh was remarking on how going back to Swift code just wasn’t clicking, and he found it easier to just write stuff in Objective-C.  So I thought, hey, until Google updates their docs, why not list some Swift code samples for how to implement various things in Swift.

Here I just want to look at a simple task: Event tracking in Google Analytics.  Yes, this might seem trivial, but when you’re a developer switching back and forth between Objective-C and Swift, sometimes it’s nice if someone points out the syntax, as per Aaron’s comments.

Here’s the Objective-C code (adapted from the Google Docs, link above):

[tracker send:[[GAIDictionaryBuilder createEventWithCategory:@"ui action"
                                                      action:@"button press"
                                                       label:@"do something"
                                                       value:@1988] build]];

And here’s the Swift equivalent:

tracker.send(GAIDictionaryBuilder.createEventWithCategory("ui action", action: "button press", label: "do something", value: 1988).build() as [NSObject : AnyObject])

btw if anyone comes up with, or knows of, a nice plugin for WordPress to color code Swift appropriately, please let me know! 😛

 

Links:

core data & cloud services for iOS in Swift

As an iOS / Swift mentor for Thinkful.com, every week I give my students a list of resources as a follow up to our mentoring session.  This week’s resources cover details of dealing with data loaded from the Internet, storing data locally (core data and alternatives to core data), and cloud services.  Finally we wrap up with some photoshop tips to automatically create 1X, 2X, and 3X assets for your iOS projects.

 

this week’s iOS related resources

tools of the trade

This week’s resources relate to putting together your UI in Interface Builder – Auto Layout, Adaptive Layout, Size Classes, Dynamic Text, and mastering the Interface Builder IDE.  Additionally, we touched on architectural design patterns (MVC, MVVM, etc.).  Finally, most of my students are starting to struggle with networking via the AFNetworking library, and using Objective-C libraries (although none of them know any Objective-C) in their Swift apps using bridge headers. Continue reading “this week’s iOS related resources”

refactor your heart (or, “how not to collapse on the road with cramping triceps while trying to learn the Swift Programming Language”)

Focus beyond the target

I came across this post by Janie Clayton-Hasz where she paints a frightening picture of her spending every waking hour, plus hours where she is supposed to be sleeping, engaged in an epic struggle to ingest the vast sea of knowledge that is the iOS development platform.  I didn’t get it; I asked her on twitter why she didn’t just slow down.  She replied, “I don’t feel like I can…am trying to catch up to everyone!”  If you’re a software developer, I’d recommend you read her post.  In fact, that led me to Ed Finkler’s post, Marco Arment’s post, and Matt Gemmel’s post.  Ever smell that burning scent when you’re driving and suddenly realize the handbrake has been engaged all along?

With the exception of Matt, I had a hard time conceptualizing their frustrations.  At least in Matt’s case, he realized at some point that the dev side of things wasn’t what drove him, so he switched to something that did.  I respect Matt’s perspective, though I don’t share his cup of tea.  I tried stepping away from dev, too – not out of frustration, but because I wanted to try taking a step back and just focus on managing my company.  I failed miserably – but in the process I realized that tech is what I love, dev is what I love – I have an innate need to stay in the midst of the code melee.

Not too long ago I went on an eighty-mile ride with my cycling buddies.  These aren’t a bunch of young guys with hopes of riding pro – these are all professionals, most of them middle-aged, majority of them physicians, mind you.  One guy is the VP of a sizable Virginia tech company.  My typical daily ride is about twenty miles.  The first twenty miles of this eighty mile ride, I was sprinting every climb.  I felt awesome.  I was leaving stronger, younger riders in the dust, in fact we almost dropped one of them.  Around mile sixty, I collapsed on the side of the road screaming in pain.  No, it wasn’t my legs – my triceps were completely cramped.  Sound kind of stupid?  Yeah, it was.  Lucky for me one of the guys was patient and kind enough to stick with me when everyone else dropped me (no, not one of the doctors – it was the VP!), and even gave me his bottle when my drink ran out.  Point being, I was an idiot for not focusing on the overall goal – getting to the end of that 80 miles, I failed to pace myself and I started burning out after 20 miles, and by 60 miles I was done, and the last twenty miles, I crawled to the end by sheer force of stubborn will.

Are you a developer feeling burned out trying to keep up with the ever changing landscape of technology?  For me, the constant change is what drives me, motivates me, and keeps me interested.  Otherwise, I would burn out, from sheer boredom.  Since transitioning from Flex development / Flash platform to iOS, things have ramped up considerably, too.  iOS isn’t just a new platform, for me, I’ve found that the depth and breadth of the platform go way beyond what I was used to.  Yeah, my bike rides and my drives are filled with listening to any one of eight different iOS-related podcasts.  I am concurrently reading four different iOS-related books.  And I dragged myself from my work-at-home life to commute eleven hours each way to work – a sacrifice I made for an iOS dev opportunity.  Up until Ramadan, when I ate breakfast in the morning it was in the company of WWDC 2014 videos.  But – none of this is work for me.  I enjoy this stuff.  I eat it up.  If I found it boring, or unexciting, as some of the aforementioned bloggers seem to feel, I would find something else to do.  That’s happened to me before – and I know myself – when stuff gets boring, I need to change things up or I fail miserably.

I still find time to ride my bike.  When I’m not riding, it’s not work that’s keeping me from it, its another non-work related activity (like taking my kids biking!).  I still find time to spend with my family.  And so on.  Yeah, I’m eating and sleeping iOS – but I’m still sleeping at night.  I still practice iaido, try to squeeze in another martial art or two and I still listen to at least thee other podcasts that have nothing to do with iOS.  No, I don’t find time to do everything – but I realize my time is precious and so I prioritize.

I guess what I’m trying to say is, to me, this stuff is all a matter of perspective. I don’t see any reason to get bent out of shape about work, or any reason or desire to “keep up” with what other folks are doing.  Everyone has their own strengths and weaknesses – there’s no need to compete with folks, rather be smart about knowing your own weaknesses and finding the right allies to help you where and when you need help.  I choose my own pace, and I feel like everyone else in the world can too, if they just make a conscious decision to do it, instead of letting the spectre of work, earning a living, a boss, etc. choose the pace for you.  Bottom line is, fix your priorities, and don’t make earning money one of those priorities, unless you actually want to lead a miserable existence.

OK, so if you’ve bother to read this far, you might be thinking, this is all nice talk, but it’s just talk.  In the real world people need to earn money to survive, and that means spending all your time playing catch up, because I got in the game late, because I need to be THE expert to get hired, and so on…there is no shortage of excuses.

When I was in grad school, I also worked part-time tutoring adult students at a community college in the inner city of Chicago.  These are mostly folks who never had time to go to college during the time that us other ‘privileged’ folks went to school – they spent their childhoods working three jobs to support their ten siblings and ailing single mom.  Now, as adults, they came back to school to get some sort of degree, to help make them more competitive in the work force.  These are driven folks, not burnt out folks.  One lady had her own shoe company – a real entrepreneur.  But the task of learning things like basic algebra, as an adult, is not an easy one.  One can feel like they are too old, the brain is too mature, and overall the prospect of learning can be daunting.

But, it’s all in your head.  This is one critical lesson I learned in my early days of martial arts training.  The first time a student has to break a board in Taekwon Do class, the board can seem like an insurmountable barrier.  It’s solid, it’s a tree that has spent a hundred years forming from the soil.  It’s a barrier against which your flesh and bones will be crushed.

Our TKD teacher taught us to realize, however, that in reality, the board is not actually there.  Once you visualize that board not as a board, but as a layer of water, or a layer of air, your focus completely changes – from the board itself, to the point beyond the board, where you are supposed to be focused.  For the student who focuses on the board, his hand stops at the board.  But for the student who focuses beyond the board, her hand passes through with zero effort, to the actual point of her focus.  And I swear to you from experience – if you focus properly, you don’t even feel the board – in fact sometimes you have to be careful of hitting the guys behind the board.

I taught this concept to my community college students.  Don’t focus on the problem – focus beyond it.  And it worked.  We shattered mental barriers.

About a decade and a half ago, when my wife was late in her second pregnancy, I was contracting for a consulting company contracting for a failing tech startup in Baltimore.  The contracting company had a ton of us “java developers” sitting in seats writing VB code, with the supposed intention that we were going to migrate their systems from VB to Java.  Sitting behind me was another developer who’s wife was also expecting.  He sat at his desk loyally tapping away at that keyboard until the moment his wife went into labor, he rushed home to take her to the hospital, and a half hour after the delivery, he was back at his desk.  In contrast, I let my manager know that I was planning on taking a break – about three weeks before my wife’s due date and a couple weeks after.  I had an hour commute to the project site, I don’t think there was any concept of telecommuting back in those days, and I wanted to make sure I was around to help my wife take it easy, get her to the hospital on time, and be there for my family in that critical time.  My manager said that the client was keeping a keen eye on the seats they were occupying, and that if I took time off, my seat might not be there anymore when I wanted to come back.  I said that was fine by me – I didn’t even have to think twice about it.  The guy who sat behind me and I, we had quite different priorities.

Life is an illusion.  Life is short.  If you are too busy with your nose on the grindstone – a grindstone that isn’t even real, mind you, you’re not only not going to smell the roses, they’re going to get crushed in that fake grindstone.

Don’t get me wrong, nobody’s perfect and we all make mistakes.  In fact, lots of mistakes.  That’s kind of the point – it’s how we grow – by learning from our mistakes.  And to Janie I’d like to say – nobody’s looking in the rear view mirror for you – in fact it’s probably the opposite.  I came to IT after a decade working in biotech labs trying to make molecular assays for HIV, and at least half the folks I know in the biz aren’t those young kids who grew up with keyboards in their cribs, either.  You’re speaking at iOS conferences – that’s an accomplishment!  I get that you love the platform – so do I – but get some sleep – you might be surprised how much more effective you are the next day…