Objective C #3 – Structures


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: , , ,


3 Responses to Objective C #3 – Structures

  1. haresh says:

    its good for learner of objective-c but can u provide all the book in same as contents given below

  2. Max says:

    Great. Nice straightforward explanation. Thank you.

  3. Nosy Rat says:

    Nice information. Will use structs.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: