I was just reading a Slack channel I’m a part of and someone was asking if it’s difficult to add Swift to an Objective-C project. It’s not. Basically you should add a bridging header to your project so you can call your Objective-C code from Swift then do a full nullability audit on any code you’d like to call from Swift. That’s it in a nutshell as far as I can remember.
Another thing to keep in mind as you start writing Swift. Just start writing code. It doesn’t matter if it’s “swifty” or not. I’m an old C/C++ developer and people probably think my Objective-C and Swift look like an old C/C++ developer wrote it. Sure, there are nice things in the language you’ll learn to take advantage of but, to get started, just write code.
If you’ve been developing for any period of time you’ll understand that the debugger is your friend. This tip is probably not new to a lot of folks, but I thought I’d share for anyone new to Xcode, Cocoa, and Objective-C development.
The Conditional Breakpoint
We’ve all set breakpoints in Xcode while debugging, but did you know you can set conditional breakpoints? Let’s say you’re processing a large quantity of data and you say to yourself “Man, I wish I could break right here when the value of this string is ‘labs'” Guess what, you can. Two easy steps
Step #1: Right click (Command-Click) on the breakpoint and select Edit Breakpoint…
Step #2: In the popover type “(BOOL)[documentName isEqualToString:@”labs”]” into the Condition field.
That’s all you need to do! In this case the breakpoint will be skipped until documentName is “labs.”
I just ran across something that had me stumped for a while, I just couldn’t see it, and I would have thought the compiler would have choked on it. It didn’t, it built, and ran, and produced interesting results.
Confession time: I’m not using auto layout, yet. Why? I haven’t had the time to commit to it. Yes, I’m aware it would probably save me time in the long run. I will learn it, of course, just not today.
Anywho, back to the story.
I hade some code that looked like this, I must have been distracted mid-thought, and did a build. This code built.
self.thingView.frame = frame;
Anyone see a problem there? No assignment, no closing “;”. The code built and ran! The outcome was my view moving into strange positions during rotation for no apparent reason. Wow.
I finally had to do a diff to find it, I just couldn’t see it.
Krzysztof ZabÅ‚ocki: “I donâ€™t like passing around JSON so I write parsing on top of native objects like NSDictionary/NSArray. If you get data as JSON just write a simple category that transforms JSON to native objects using NSJSONSerialization.”
Here’s a nice hunk of code that will save you some time when you write your next iOS App that talks to a web service. I don’t like passing around NSArray or NSDictionary either, or even worse the raw JSON you get back from a service. I’ve written a few times about transforming JSON into an Object, and it’s not hard to do, but it’s so “boilerplate” it feels like a waste of time. Time you could use elsewhere. Krzysztof provides a nice way to get past all that boilerplate code and get automagic serialization of JSON to Object. It’s definitely worth a look and worth understanding the pattern.
The New York Times, Bits: “The current version of the app is essentially an Objective-C shell with a Web browser inside. When it comes to speed, this is like putting the engine of a Smart Car in the body of a Ferrari.“
That’s a decent way to describe wrapper apps.
Mike Ash: “That, in essence, is what ARC is. The memory management rules are baked into the compiler, but instead of using them to help the programmer find mistakes, it simply inserts the necessary calls on its own.”
Reference counting is a pain in the keister, until you understand the rules. ARC is going to become an important tool in the quest to creating error free code. If you’ve ever written ref counted code you know how careful you need to be so you don’t leak memory or double-release something and cause a crash. It can be frustrating if you don’t pay attention.
This is a welcome change and one I’m looking forward to.
I think it’s time for an RxCalc refresh anyway. Plus some other nifty things I’ve been working on, that will definitely be ARC’d and Mac OS X Lion / iOS 5 only.