The @ directive (iOS 4)

We’re almost finished with our overview of Objective-C, but we want to alert you to one other frequently used bit of syntax. As you’ve seen, the @ symbol denotes a compile directive. It’s a core part of class definition, and it’s required for properties. You’ll also see it in a few other places in Objective-C code.

Sometimes an @ is used to create variables of certain types, most frequently a variable of type NSString *. You saw this in a few of the messaging examples. You include the @ symbol, followed by the string value you want to set:

tmp1239_thumb

In topic 6, you’ll also encounter the @selector directive, which is used to create a variable of type SEL. This is a method selector, which is what you use when you want to pass the name of a method as an argument, as will occur when we get to events and actions. A standard usage looks like this:

tmp1240_thumb


There are many other directives you can use in Objective-C. Our purpose here is to highlight those you’re most likely to see in this topic and most likely to use in introductory SDK programming.

TIP Forgetting to mark a string with an @ is the most common error in iOS programming, so keep an eye out for this one! (Without the you’ll have a C-style char* string, which is almost certainly not what you want.)

Next post:

Previous post: