Structures

The Objective-C language provides another tool besides arrays for grouping elements. You also can use structures, which form the basis for the discussions in this section.

Suppose you wanted to store a date—say, 7/18/11—inside a program, perhaps to be used for the heading of some program output or even for computational purposes. A natural method for storing the date is to simply assign the month to an integer variable called month, the day to an integer variable day, and the year to an integer variable year. So, the statements

int month = 7, day = 18, year = 2011;

would work just fine. This is a totally acceptable approach. But what if your program also needed to store several dates? It would be much better to somehow group these ...

Get Programming in Objective-C, Fifth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.