Objective C #3 - Structures

September 27, 2006 by Gabe | [mmd] |

ObjectiveC

It's been awhile since I posted about my journey into Objective C and Cocoa programming. It's going a lot slower than I had hoped, but I up to page 260 in "Programming in Objective C" so at least I'm not a complete slacker. It doesn't help that most of the really basic material is as exciting as a wet shoelace. But today on my commute to work I read about Functions and Structures. This is pretty darn cool stuff. Functions are really just quick and easy methods, but Structures are where it really gets interesting. A structure is a way to group related variables together and then assign or recall values really quickly. Here is an example from the book:

struct date
{

int month;
int day;
int year;

}

This bit of code intializes a set of variables named month, day and year which comprise the variable called date.

struct date today;

Now we have declared a new date named "today." Here's the cool part. It's really easy to set the day and year of your date as follows:

today.day= 21
today.year = 2003

That's it. Now your date variable contains a day and year that can be recalled and reasigned as needed. The period simply says to access the sub variable "day"

You can even set the entire date like this

struct date today = { 7, 2, 2004 };

I'll try to add some more later this week. The other thing I found pretty interesting is the use of Definitions (#define).

Technorati Tags: , , ,

blog comments powered by Disqus