Referenced from: http://developer.apple.com/mac/library/referencelibrary/GettingStarted/Learning_Objective-C_A_Primer/index.html#//apple_ref/doc/uid/TP40007594
=====================================================
Learning Objective-C: A Primer
Learning Objective-C: A PrimerThe Objective-C language is a simple computer language designed to enable sophisticated object-oriented programming. Objective-C extends the standard ANSI C language by providing syntax for defining classes, and methods, as well as other constructs that promote dynamic extension of classes. (This document doesn’t attempt to teach any aspects of C itself. If you’re not familiar with the language, you should learn about the basics before you proceed.) If you have programmed with object-oriented languages before, the following information should help you learn the basic syntax of Objective-C. Many of the traditional object-oriented concepts, such as encapsulation, inheritance, and polymorphism, are all present in Objective-C. There are a few important differences, but those differences are called out in this article and more detailed information is available if you need it. If you have never programmed using an object-oriented language before, you need to have at least a basic understanding of the associated concepts before proceeding. The use of objects and object-oriented design patterns is fundamental to the design of Cocoa applications, and understanding how they interact is critical to creating your applications. For an overview of concepts, see Object-Oriented Programming with Objective-C . In addition, see Cocoa Fundamentals Guide for information about the design patterns used in Cocoa. For full details of the Objective-C language and syntax, see The Objective-C Programming Language . Objective-C: A Superset of CObjective-C is a superset of the ANSI version of the C programming language and supports the same basic syntax as C. As with C code, you define header files and source files to separate public declarations from the implementation details of your code. Objective-C header files use the file extensions listed in Table 1 .
When you want to include header files in your source code, you typically use a ClassesAs in most other object-oriented languages, classes in Objective-C provide the basic construct for encapsulating some data with the actions that operate on that data. An object is a runtime instance of a class, and contains its own in-memory copy of the instance variables declared by that class and pointers to the methods of the class. The specification of a class in Objective-C requires two distinct pieces: the interface and the implementation. The interface portion contains the class declaration and defines the instance variables and methods associated with the class. The interface is usually in a Figure 1 shows the syntax for declaring a class called Figure 1 A class declaration ![]()
Note: Although this class declaration declares only methods, classes can also declare properties. For more information on properties, see
“Declared Properties” .
When storing objects in variables, you always use a pointer type. Objective-C supports both strong and weak typing for variables containing objects. Strongly typed pointers include the class name in the variable type declaration. Weakly typed pointers use the type The following example shows strongly and weakly typed variable declarations:
Notice the Methods and MessagingA class in Objective-C can declare two types of methods: instance methods and class methods. An instance method is a method whose execution is scoped to a particular instance of the class. In other words, before you call an instance method, you must first create an instance of the class. Class methods, by comparison, do not require you to create an instance, but more on that later. The declaration of a method consists of the method type identifier, a return type, one or more signature keywords, and the parameter type and name information. Figure 2 shows the declaration of the Figure 2 Method declaration syntax ![]() This declaration is preceded by a minus ( When you want to call a method, you do so by messaging an object. A message is the method signature, along with the parameter information the method needs. All messages you send to an object are dispatched dynamically, thus facilitating the polymorphic behavior of Objective-C classes. Messages are enclosed by brackets (
To avoid declaring numerous local variables to store temporary results, Objective-C lets you nest messages. The return value from each nested message is used as a parameter, or as the target, of another message. For example, you could replace any of the variables used in the previous example with messages to retrieve the values. Thus, if you had another object called
Objective-C also provides a dot syntax for invoking accessor methods . Accessor methods get and set the state of an object, and typically take the form
You can also use dot syntax for assignment:
This is simply a different syntax for writing, Although the preceding examples sent messages to an instance of a class, you can also send messages to the class itself. When messaging a class, the method you specify must be defined as a class method instead of an instance method. You can think of class methods as something akin to (but not exactly like) static members in a C++ class. You typically use class methods as factory methods to create new instances of the class or for accessing some piece of shared information associated with the class. The syntax for a class method declaration is identical to that of an instance method, with one exception. Instead of using a minus sign for the method type identifier, you use a plus (+) sign. The following example illustrates how you use a class method as a factory method for a class. In this case, the
Listing 1 shows the implementation of Listing 1 A class implementation
Declared PropertiesDeclared properties are a convenience notation used to replace the declaration and, optionally, implementation of accessor methods. You include property declarations with the method declarations in your class interface. The basic definition uses the
Each readable property specifies a method with the same name as the property. Each writable property specifies an additional method of the form In your class implementation, you can use the
You can combine the
Practically speaking, properties reduce the amount of redundant code you have to write. Because most accessor methods are implemented in similar ways, properties eliminate the need to implement a getter and setter method for each property exposed in the class. Instead, you specify the behavior you want using the property declaration and then synthesize actual getter and setter methods based on that declaration at compile time. For information on how to declare properties in your own classes, read Declared Properties in The Objective-C Programming Language . StringsAs a superset of C, Objective-C supports the same conventions for specifying strings as C. In other words, single characters are enclosed by single quotes and strings of characters are surrounded by double quotes. However, most Objective-C frameworks do not use C-style strings very often. Instead, most frameworks pass strings around in The
Protocols and DelegatesA protocol declares methods that can be implemented by any class. Protocols are not classes themselves. They simply define an interface that other objects are responsible for implementing. When you implement the methods of a protocol in one of your classes, your class is said to conform to that protocol. Protocols are used frequently to specify the interface for delegate objects. A delegate object is an object that acts on behalf of, or in coordination with, another object. The best way to look at the interplay between protocols, delegates, and other objects is to look at an example. The The declaration of a protocol looks similar to that of a class interface, with the exceptions that protocols do not have a parent class and they do not define instance variables. The following example shows a simple protocol declaration with one method:
In the case of many delegate protocols, adopting a protocol is simply a matter of implementing the methods defined by that protocol. There are some protocols that require you to state explicitly that you support the protocol, and protocols can specify both required and optional methods. As you get further into your development, however, you should spend a little more time learning about protocols and how they are used by reading Protocols in The Objective-C Programming Language . For More InformationThe preceding information was intended primarily to familiarize you with the basics of the Objective-C language. The subjects covered here reflect the language features you are most likely to encounter as you read through the rest of the documentation. These are not the only features of the language though, and you are encouraged to read more about the language in The Objective-C Programming Language . Last updated: 2009-06-25 |
<!-- start of path -->
<!-- end of path -->