Storing CGPoint, CGSize and CGRect in Collections using NSValue

In an earlier post CGRect, CGSize and CGPoint Functions I demonstrated a number of geometry structures available for representing a point (CGPoint – x and y coordinates), size (CGSize – height and width) and rectangles (CGRect – combination of both).

Unfortunately, you cannot directly store any of the above in a collection, for example an array, as each of these is a C structure, not an object. In this post I will show you how to store CGPoint CGRect CGSize in an Objective-C collection.

Continue reading

How to Use NSLog to Debug CGRect and CGPoint

CGPoint and CGRect are structures (versus objects) and therefore the old NSLog standby %@ will not work as expected.

Here is how each structure is defined:

struct CGPoint {
   CGFloat x;
   CGFloat y;
};
typedef struct CGPoint CGPoint;
 
struct CGRect {
   CGPoint origin;
   CGSize size;
};
typedef struct CGRect CGRect;

If you attempt NSLog to use the traditional ‘print object’ notation such as this:

// Print point structure using NSLog
CGPoint cgPoint = CGPointMake(1, 11);
NSLog(@"%@", cgPoint);

the compiler will generate a warning: Format specifies type ‘id’ but the argument has type ‘CGPoint’ (aka ‘struct CGPoint’)

Good news is, this is easy to fix:

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