NSArray Literals In Objective-C

When running Xcode 4.4 (the latest download from the Mac App Store) I noticed the option for Apple LLVM Compiler 4.0:

One reason this is significant is that you can now define literals for a number of additional objects, similar to how you define string literals with the @ prefix. In this post I’ll show you how to use NSArrays literals. Let’s look at a few examples:

// Previous format to define empty array
NSArray *emptyArray = [NSArray array];
 
// Previous format to define single element array
NSArray *singleElementArray = [NSArray arrayWithObject:@"Error Message"];
 
// Previous format to define array of strings
NSArray *arrayOfStrings = [NSArray arrayWithObjects: @"First Name", @"Last Name", nil];

The new NSArray literal format follows:
Continue reading

Converting Radians to Degrees and Degrees to Radians

In mathematics, a radian is often used as a standard unit of measure when expressing angles. For various methods in iOS classes that deal with angles and rotation, the parameters and return values are often in radians. My brain doesn’t think in radians, I visualize angles and rotations in degrees, hence the code in this post to convert between the two.

In the post Rotate an Image with Animation I wrote a method to rotate an image a specified number of degrees, however the method that does the work, CGAffineTransformMakeRotation, requires a radian value as input.

Continue reading

Of BOOL and YES

It may not be immediately clear that the Objective C BOOL "type" is not actually a boolean type at all. This is a legacy from the original C language, which does not have an intrinsic boolean type (the iPhone GCC C compiler supports the ISO C99 standard which does define a bool type). To clarify, in the system header file objc.h you can see the following:
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

Conditional Compilation

In the previous post I used conditional compilation to enable and disable debugging messages, yet I didn’t take the time (for those new to C/Objective-C) to explain how everything worked.

There are three ways to control compilation, the options are as follows:

1
2
3
#if constant-expression
#ifdef identifier
#ifndef identifier

Continue reading

Where is My Object Retained?

For those new to Objective-C and iPhone development, I want to point out something that might save you some time. Look at the code below:

1
2
3
4
5
6
@interface SomeClass : UIViewController
{
  ..
  UIView *containerView;
  ...
}
1
2
3
4
5
6
7
8
9
10
11
12
13
@implementation SomeClass
...
- (void)loadView
{
  ...
  UIView *uiView = [[UIView alloc] initWithFrame:
     [[UIScreen mainScreen] applicationFrame]];
  self.containerView = uiView;
  [uiView release];
  ...
}
...
@end

Continue reading

Alternative Use of Properties?

While scanning a few code examples inside the iPhone SDK, I bumped into something interesting. I wrote a short example to mimic what I found…take a look at the following interface declaration:

1
2
3
4
5
6
7
8
9
@interface SomeClass : NSObject
{
  NSString *str;
  NSDate *date;
} 
 
@property (nonatomic, retain) NSString *str;
@property (nonatomic, retain) NSDate *date;
@property (readonly) NSString *testSomething;

The only aspect that should standout is the property declaration for testSomething . Notice there is no instance instance variable tied to this declaration. This is different if for no reason other than the typical use for properties is as a shorthand for declaring accessor methods for instance variables.
Continue reading