Aug
12
2010
If you ever come upon a need to toggle an integer value between 1 and 0, consider using the bitwise exclusive-OR (^) operator in C to get the job done.
In a recent application I wrote a method with one parameter, an integer, that is expected to be 1 or 0. In creating a demo of the application I wanted to pass in alternating values of 1 and 0 as part of a test for a specific use case. Instead of using an if statement in the calling method to decide when to send a 1 or 0, I wrote something similar to the code below:
Continue reading...
Aug
09
2010
Given space is often at a premium when writing applications for mobile devices, I want to show you how to use bitfields to manage a series of values that need only on/off status. The upside is that you can store a surprising number of status values within a single integer, 32 to be exact.
The code that follows is pure C. Given that Objective-C is a superset of C, don’t forget that you can leverage all that C has to offer, beyond working with objects.
Continue reading...
Apr
26
2009
What follows is a quick introduction to working with protocols. This is good background information to understand as protocols are common in various Cocoa frameworks. A protocol is means to define a list of required and/or optional methods that a class implements. If a class adopts a protocol, it must implement all required methods in the protocols it adopts.
Cocoa uses protocols to support interprocess communication through Objective-C messages. In addition, since Objective-C does not support multiple inheritance, you can achieve similar functionality with protocols, as a class can adopt more than one protocol.
Continue reading...
Apr
14
2009
What follows is a brief guide to working with Notifications in Cocoa. I’ll cover the basics, including registering an observer and posting notifications, just enough to start using notifications in your apps.
There is an instance of NSNotificationCenter available to every running application. This class acts as an intermediary to facilitate communication between objects that are interested in being notified at some point in the future (these objects are known as the observers) and a poster that posts to the notification center, resulting in all observers (registered for a specific notification) being called.
Continue reading...
Apr
06
2009
Subtle typos and the problems they bring, it’s enough to drive one nuts. Let me share a recent experience that will shed some light on my most recent experience. Below is a short snippet of code that show something similar to what I was recently attempting to do. It’s nothing more than a variable definition, and in one place incrementing the variable, in another I decrementing.
Continue reading...
Mar
26
2009
In the previous post I described the basics for working with code completion in Xcode. In this post I will show how you can use built-in text macros to insert various code fragments.
As an example, begin by entering ifelse into Xcode and follow this by pressing Control . (control period) and you’ll see the following code block inserted:
Continue reading...
Mar
05
2009
What follows is a quick review of how I use code completion in Xcode. Chances are that options and features exist beyond what I’ll cover here, so comments and suggestions are welcome.
Let’s say I want to insert a CGRectMake method. I can begin by typing CG and pressing F5 (or Option-esc), which will popup a list of possible matches:
Continue reading...