天天看點

Objective-C Basics 1

1. 

the special Objective-C self parameter. Each method has an implicit self parameter (it’s not in the method declaration), which is a pointer to the object that received the message.

2.

Class factory methods are convenience methods used to perform class creation and initialization. They are class methods that are typically named following the convention

+ (id) className...      

className is the name of the class and begins in lowercase. 

3.

+ (id) hydrogenWithNeutrons:(NSUInteger)neutrons
{
  return [[[self class] alloc] initWithNeutrons:neutrons];
}      

Also notice the use of the expression [self class] to retrieve the current class instance. Using this expression (as opposed to specifying the class directly), if the class is subclassed and the factory method is invoked by a subclass, the instance returned is the same type as the subclass.

4.

Class methods work the same way as instance methods, except that they are invoked on a class, not an object. As a result, class methods do not have access to the instance variables and methods defined for the class. They are commonly used to create new instances of a class (i.e., as factory methods), or for accessing shared information associated with a class.

5. 

Program Memory Usage

In order to better understand the role of memory management, you need to understand how a program is stored and used in computer memory. An executable Objective-C program is composed of (executable) code, initialized and unitialized program data, linking information, relocation information, local data, and dynamic data. The program data includes statically-declared variables and program literals (e.g., constant values in the code that are set during program compilation).

The executable code and program data, along with the linking and relocation information, are statically allocated in memory and persist for the lifetime of the program.

Local (i.e., automatic) data consists of data whose scope is delimited by the statement block within which it is declared and is not retained after the block has been executed. Syntactically, an Objective-C compound statement block is a collection of statements delimited by braces (as shown in Listing 4-1).

Listing 4-1.  Example Statement Block

{
  int myInt = 1;
  float myFloat = 5.0f;
  NSLog(@"Float = %f, integer =  %d", myFloat, myInt);
}      

The example in Listing 4-1 includes two local variables, myInt and myFloat, which exist within this statement block and are not retained after the block has been executed.Automatic data resides in the program stack, a block of memory whose size is usually set prior to program/thread execution. The stack is used to store local variables and context data for method/function invocation. This context data consists of method input parameters, return values, and the return address where the program should continue execution after the method completes. The operating system automatically manages this memory; it is allocated on the stack for and subsequently deallocated at the end of the scope where it was declared.

At runtime, an Objective-C program creates objects (via the NSObject alloc method, as discussed in Chapter 3) stored in dynamically allocated memory referred to as the programheap. Dynamic object creation implies the need for memory management, because objects created on the heap never go out of scope.

A diagram depicting the allocation and usage of memory during program execution is shown in Figure 4-1.

Objective-C Basics 1

Figure 4-1. Objective-C program memory usage