Objective-C
iPhone Developer Tips Stats:
|
Fast Enumeration on the iPhone
With the introduction of Objective-C 2.0, you can now iterate through collections (arrays, etc) with ease using a language feature known as Fast Enumeration.
This enumeration is both faster than using NSEnumerator and is also much more concise as it relates to the syntax, resulting in code that is easier on the eyes.
Specifying Simulator Only Code
There are times when you need to write code that is only applicable when working with the simulator.
As an example, I was recently working with an application that required the user to take a picture with their iPhone. Once the picture was taken the application was to show the image to the user, at [...]
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, [...]
Class Variables
I previously wrote about the lack of support for private methods when working with Objective-C. As part of that post I presented a few work-arounds. Along the same lines, there are is no support for class variables in Objective-C. This post will explore this a little further and walk through a short example that shows [...]
Objective-C Object as a C Structure
Okay, so figuring out how to unwind an Objective-C object into its base representation goes against all that is object-oriented programming, however, it’s interesting none-the-less.
In Objective-C there is a directive, @defs(), that outputs (at compile time) the list of instance variables for a class. We can use this list to create a C structure [...]
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 [...]
Java Developer’s Guide to String Constants in Objective-C
It is my experience that, when developing a J2ME application, excessive String creation is often the leading cause of memory “leaks”. There is always that step during the development cycle where you go back over all your code and refactor out any strings that should be defined as constants. This is no less [...]
Java Developer’s Guide to Static variables in Objective-C
The concepts of “static” variables are different in Objective-C compared to Java. That does not mean, however, that the concepts do not exist.
Consider the following common uses of ’static’ in Java:
public static final int MY_CONSTANT = 0; // Constant
public static String MyVar = “Foo”; // Class Variable
In all of the above cases, the use [...]
A Java Developer’s Guide to Threads on iPhone
Java makes it ridiculously simple to run code in threads and ensure that your objects are thread safe. When developing user experiences, threads enable you to do things in the “background” (like downloading and parsing content) without rendering your application unresponsive and giving the user the sense that the application may have “frozen”. [...]
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
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 [...]
NSIndexPath and row method
In working with the SimpledDrillDown example in the iPhone SDK, I was walking through the code in the RootViewController class. The interface definition is shown below:
1
2
3
4
@interface RootViewController:UITableViewController
{
DataController *dataController;
}
There is a method inside the class that looks as follows:
1
2
3
4
5
6
7
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
…
detailViewController.detailItem = [dataController objectInListAtIndex:indexPath.row];
…
}
The last parameter to this method ‘indexPath’ is of type [...]
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 [...]
Properties, Setters and Dot Syntax
I’ve been ramping up on iPhone development, and with the NDA still in place (as far as I know), I haven’t been able to blog about what I’ve written/learned. And with that, it’s been quiet in here. Too get back into this, let’s continue to spend some more time on Objective-C…
The properties feature in Objective-C [...]
Private Methods
One common complaint for many new to Objective-C is that the language lacks support for private methods within a class. There are compiler directives for instance variables: @private, @protected (the default) and @public. However, if you want to hide methods, there is no specific directives to help. The example here builds on the previous post [...]








