Objective-C Programming_ The Big Nerd Ranch Guide - Aaron Hillegass [26]
#include #include int main(int argc, const char * argv[]) { // Declare a pointer float *startOfBuffer; // Ask to use some bytes from the heap startOfBuffer = malloc(1000 * sizeof(float)); // ...use the buffer here... // Relinquish your claim on the memory so it can be reused free(startOfBuffer); // Forget where that memory is startOfBuffer = NULL; return 0; } startOfBuffer would be a pointer to the first floating point number in the buffer. Figure 11.1 A pointer on the stack to a buffer on the heap At this point, most C books would spend a lot of time talking about how to read and write data in assorted locations in that buffer of floating pointer numbers. This book, however, is trying to get you to objects as quickly as possible. So, we will put off C arrays and pointer arithmetic until later. You can also use malloc() to claim space for a struct on the heap. For example, if you wanted to allocate a Person struct on the heap, you might have a program like this: #include #include typedef struct { float heightInMeters; int weightInKilos; } Person; float bodyMassIndex(Person *p) { return p->weightInKilos / (p->heightInMeters * p->heightInMeters); } int main(int argc, const char * argv[]) { // Allocate memory for one Person structure Person *x = (Person *)malloc(sizeof(Person)); // Fill in two members of the structure x->weightInKilos = 81; x->heightInMeters = 2.0; // Print out the BMI of the original Person float xBMI = bodyMassIndex(x); printf("x has a BMI of = %f\n", xBMI); // Let the memory be recycled free(x); // Forget where it was x = NULL; return 0; } Notice the operator ->. p->weightInKilos says, “Dereference the pointer p to the structure and get me the member called weightInKilos.” This idea of structures on the heap is a very powerful one. It forms the basis for Objective-C objects, which we turn to next. Part III Objective-C and Foundation All Objective-C programming is done with the Foundation framework. A framework is library of classes that you use to write programs. What’s a class? That’s what we’ll talk about first… 12 Objects In the early 1980’s, Brad Cox and Tom Love decided to add object-oriented ideas to the C language. For objects, they built upon the idea of structs allocated on the heap and added a message-sending syntax. The result was the language Objective-C. Objects are very chatty by nature. They do work and send and receive messages about the work they are doing. A complex Objective-C program can have hundreds of objects in memory all at once, doing work and sending messages to each other. A class describes a particular type of object. This description includes methods and instance variables where an object of this type stores its data. You ask a class to create an object of its type for you on the heap. We say that the resulting object is an instance of that class. For example, an iPhone ships with many classes, one of which is CLLocation. You can ask the CLLocation class to create an instance of CLLocation. Inside this CLLocation object are several instance variables that hold location data, like longitude, latitude, and altitude. The object
Now that you have an understanding of the basics of programs, functions, variables, and data types, you are ready to learn Objective-C. We’ll stick with command-line programs for now to keep the focus on programming essentials.
Many computer languages have the idea of objects. An object is like a structure in that it holds data. However, unlike a structure, an object also contains a set of functions that act upon that data. To trigger one of these functions, you send a message to the object. To use the correct word, a function that is triggered by a message is known as a method.