BuySellAds.com

Read the chapters in my new book Barcodes with iOS 7 as I hand them in. Great new app opportunities await!
Our DNA is written in Objective-C
Jump

Category Archive for ‘Recipes’ rss

And Now Lazy Loading with NSURLConnection

In my previous post I demonstrated how to quickly whip up some lazy loading for a UIImageView. Today I revamped it to use NSURLConnection instead, because this would allow for cancelling the request. It also gives us the option of specifying if we want to make use of the cache and also how long we’re actually willing to wait for an answer.

That sounds more straightforward than it actually is. If you simply use an NSURLConnection with its delegate methods then you might not see anything wrong, unless you are using this lazy image view as subview of a UIScrollView. The problem there is that the scroll view blocks the run loop and so your connection events will not get delivered until you lift the finger.

But I found a solution for that. Let me know in the comments if you think something could be done in a more elegant way. The code for this is available in the NSAttributedString+HTML project.

Read more

A Quick Lazy-Loading UIImageView

In case you ever need to enhance UIImageView to load it’s image lazily… like if it’s coming from a web URL.

I used this on my NSAttributedStrings+HTML open source project to improve performance and also be able to have images that are not available locally. But you don’t want to do a synchronous call over the web, that would block your UI.

I admit, it’s a quickie, but hey, sometimes those are also fun!

Read more

Functions as Parameters – Old & New

This topic is not for the faint of heart because it requires that you permit an additional level of abstraction in your brain to be able to grasp it. Until know all my ivars and properties where either scalar values (like an NSInteger) or pointers to Objective-C instances (like NSString *). But there are cases when you actually want to be able to store more complex functionality yet not have the overhead of object creation and messaging.

From the C days we have a mechanism called “function pointers” and today I’ll show you how you can pass a function itself to an Obj-C class and store it in an instance variable. There are a couple of SDK functions that make use of that.

The we’ll explore the modern-day equivalent of providing this sort of “plug in” dynamic functionality: doing the same thing with blocks. If you’re lucky to have iOS 4.x as minimum requirement for your project then those blocks might be nicer to work with than function pointers.

Read more

Mutability Preserverance

Apple consciously separates mutable and immutable variants of classes in Objective-C. If you have an NSString you cannot modify it, only by creating a new one with additions to an old one. If you want to mutilate mutate the string itself you have to use it’s subclass NSMutableString. Internally those are the same CoreFoundation type, yet the design choice was to have an immutable class and add mutability methods in a subclass.

There are two items that are not immediately obvious if you start out learning to program Objective-C, that I’d like to chronicle. One of these stumped me just a few days ago.

Read more

Visual View Debugging

In this article I want to summarize a couple of “barefoot techniques” for tuning your views. Sometimes you are painstakingly putting together a UI with multiple views and you cannot tell any more where one ends and another begins.

The debugger is not really working well on this because most of the interesting information about views is hidden behind properties and you cannot usefully inspect their current contents because properties are really methods the value of which is only set when you actually call them.

But I want to share some easy techniques that I started using so that I can get this visual puzzle untangled.

Read more

Splitting a String into Paragraphs … with Blocks

I found myself in need of splitting an NSString into paragraphs. Or more precisely to analyze a string and find the NSRange for each such paragraph. At first I wrote a C-style function that looked for the ‘\n’ in the NSString’s utf8String, but it turns out that this approach has problems with multi-character UTF8 sequences.

For the longest time I shirked from using Blocks, which became part of iOS with iteration 4.0. But since this project will have a minimum deployment target of higher than this, I gained the ability to use a block-based enumeration function to achieve my goal with a record minimum of code.

Read more

Cloning a Git Repo with Submodules

I was setting up a project with one submodule on my working iMac and was wondering how to do this most quickly. After tweeting the approach I had found, there where quickly some very smart people responding on how to do that better. I found this kind of crowd-sourced incremental improvement exhilarating, so I’m sharing it with you.

Read more

Expanding/Collapsing TableView Sections

While giving many designers a headache the Twitter app still serves as template on how to solve a variety of UX riddles. One of which is the situation where one might want to have sections in a tableview that possess the ability to expand from one row to several and collapse vice versa.

The eye of the experienced developer sees two challenges contained therein: 1) grafting a mechanism for collapsing and expanding onto UITableView in a reusable way 2) making custom accessory views that look like a rotated version of the disclosure indicator, pointing upwards or downwards and also changing color when highlighted.

In this article I present my solution to this UX riddle. At the same time I will demonstrate how NSMutableIndexSet can be used to our advantage. In contrast to the pull-to-reload method previously discussed, this does not contain anything remotely patentable.

Update March 12th, 2013: Cleaned up version of the custom-colored accessory is now available via DTFoundation, the example project is now part of our Examples collection on GitHub. Please note that if you use this code you have to attribute it to us or buy a Non-Attribution License.

Read more

Git Submodules and Xcode 4

Previously when integrating existing library code with new projects I would have simply copied the necessary groups from from Xcode project to another. Then I would choose to copy the files to the new project to be sure that they got included in the source tree. Otherwise the project would not build for other people accessing the same source control management (SCM) server.

Now with Xcode 4 this technique no longer works. You simply cannot drag groups between workspaces. The question that interests us today is how we can add an existing GitHub project to our own.

Let’s see if we can figure out a simple and duplicatable method to achieving this. It would make our lives much easier to not having to duplicate component code for each new project.

Read more

HTML Entities

Similar to the previous article on decoding HTML colors we also need to decode HTML entities like ". So I found an authoritative list on the web, courtesy of Wikipedia and in this article I will demonstrate how to use quickly hacked up command line tool to convert it into Objective-C.

This shall serve as an example as to how quickly you can leverage your objC knowledge to build a useful tool for such a one-off operation. If you know how to reuse your skills from iOS development on command line tools then you can always quickly whip up a one-off tool to do some work that otherwise you would have needed to do manually with a text editor.

Read more