Working with Bits and Bit Fields in C and Objective-C

We’ll cover two bit related topics in this post: getting/setting bits directly within an integer and working with bit fields in a C structure. Both have their place, it’s more about the context in which you need to use them.

The first example will be getting/setting of specific bits using an integer value along with a pre-defined set of bits we want to access. From there we will look at creating a structure that has named bit fields, making access (and code readability) much easier.

Continue reading

Ternary Operator With Objective-C Objects

I previously wrote about C ternary operatar which provides an opportunity to write code that is “short-hand” if you will. A ternary operator is one that accepts three arguments, more on this below.

This is the traditional form of an if/else:

int x = 5;
if (x > 1)
  y = x;
  y = -1;

And here is a version using the ternary operators:

int y = x > 1 ? x : -1;

Continue reading

CGRect, CGSize and CGPoint

Digging into development of iPhone applications, you’ll eventually encounter references to CGRect, CGSize, and CGPoint. These references are to C structures (see this post for more information on structures). This post will provide a high-level view of what comprises CGRect and its counterparts. Here is how CGRect is defined:
Continue reading

C Structures

Leading up to a post on working with CGRect, CGPoint and CGSize, it makes sense to visit C structures. A structure is a collection of variables, grouped together to facilitate organization of data. For example, one might define a set of x and y coordinates as follows:
Continue reading

C++ on iPhone: Part 2, Exceptions

In part 2 of this C++ on iPhone series I’ll be exploring C++ exception handling support, and as a bonus I’ll touch on use of standard C++ lib console output stream, as well as showing a way to call C++ code from Objective C. As a reminder, exception handling is normally one of the weak spots in "mobile device" C++ support, so I wanted to find out how well it is supported on iPhone.

Jumping right into some code, using the same Xcode project as in part 1, I added a new C++ class Exceptions (adding Exceptions.cpp and Exceptions.h to the project).
Continue reading