Objective-C Pocket Reference
|
| List Price: | £7.50 |
| Price: | £4.58 & eligible for FREE Super Saver Delivery on orders over £5. Details |
Availability: Usually dispatched within 24 hours
Dispatched from and sold by Amazon.co.uk
26 new or used available from £2.32
Average customer review:Product Description
This text provides a quick and concise introduction to Objective-C for programmers already familiar with either C or C++, and will continue to serve as a handy reference even after the language is mastered. In addition to covering the essentials of Objective-C syntax, it also covers important facets of the language such as memory management, the Objective-C runtime, dynamic loading, distributed objects, and exception-handling. Andrew Duncan provides a quick and concise introduction to Objective-C for the experienced programmer. In addition to covering the essentials of Objective-C syntax, he also covers important faces of the language such as memory management, the Objective-C runtime, dynamic loading, distributed objects, and exception handling. O'Reilly's "Pocket References" provide important details in a succinct, well-organized format and aim to deliver what you need to complete the task at hand.
Product Details
- Amazon Sales Rank: #24448 in Books
- Published on: 2002-12-19
- Original language: English
- Number of items: 1
- Binding: Paperback
- 128 pages
Editorial Reviews
From the Publisher
Objective-C Pocket Reference provides a quick and concise introduction to Objective-C for programmers already familiar with either C or C++, and will continue to serve as a handy reference even after the language is mastered. In addition to covering the essentials of Objective-C syntax, it also covers important facets of the language such as memory management, the Objective-C runtime, dynamic loading, distributed objects, and exception handling.
About the Author
Andrew Duncan started programming on Control Data 6600 hardware in 1974 and progressed on to Mac OS X. He holds a Bachelor's degree in electrical engineering from the California Institute of Technology, and a Masters in mathematics from the University of California at Santa Cruz. He is now on leave from doctoral work on compilers at UC Santa Barbara. He currently works at expertcity, designing the core class libraries.
Excerpted from Objective-C Pocket Reference by Andrew Duncan. Copyright © 2003. Reprinted by permission. All rights reserved.
Object Lifecycle
Your classes will have methods that distinguish them from other classes and make them useful, but all classes must implement methods that manage their lifecycle — allocation, initialization, copying, and deletion. In addition, you will use libraryclasses that come supplied with these methods, which you need to use in a consistent way. This section describes the design patterns that Objective-C programmers use and that library classes support.
The root classes Object and NSObject provide the following methods for managing the lifecycles of objects:
+initialize
+alloc
+new
-init
-copy
-dealloc
In addition,Object also provides these methods:
-shallowCopy
-deepCopy
-deepen
-free
In addition to these methods,manyclasses will provide more methods for initializing newly allocated objects.
The "Root Classes "section describes how these methods behave for the root classes; this section gives you guidelines on how to actually use the methods in your programs. In managing the lifecycle of an object,you are faced with two issues: how to call these methods and how to write them for your own classes. Each of the following sections will first discuss how to call the methods, and then how to write them.
Creating an Object
Objective-C separates object creation into two steps: allocating memory and initializing fields. Allocation returns a pointer to cleared memory where the object will be stored. Initializing an object means setting its fields to some values, either default or specified. These operations serve distinct purposes, are performed by different objects (allocation by a class, and initialization by an instance), and you write and call each of them explicitly.
Calling creation methods
To create an object, you first ask its class to allocate memory, and then you initialize the instance:
1 Circle *c =[Circle alloc ];
2 c =[c init ];
Line 1.In Objective-C you call a class method to return a pointer to memoryfor a new object. It is conventional to call this method +alloc . Both Object and NSObject supply an +alloc method. The object you get from +alloc will have all its fields set to zero.
Line 2.An initializer is an instance method that sets the fields of a newly allocated object. Every object responds to the -init message, which it inherits from the root class. The root class version does nothing, but leaves the object in its pristine just-allocated state. Descendants may override –init to provide a default initialization.
An initializer returns the initialized object, but that object maybe different from the receiver of the initialization message. For example,an initializer may fail and return nil, or substitute a proxy or other special object for the one passed in. For this reason it is not safe to call an initializer as a void method:
[c init ];//Discards return value.
In this example, any return value is discarded. If –init returns an object different from the receiver, this code will lose that object and the receiver will not be correctly initialized. To avoid this problem, chain the calls to allocate and initialize your objects, or use the new method,which does this for you (but only for the bare-bones -init method).Both
of the following lines allocate and initialize an instance of Circle :
Circle *c1 =[[Circle alloc ] init ];
Circle *c2 =[Circle new ];//Same effect.
Manyclasses will supply more initialization methods; it is conventional for their names to start with init . These methods may take additional parameters to guide the setting up of the receiver’s state.For example:
Circle *c =[[Circle alloc ]initWithRadius :3 ];
Writing creation methods
Your code should maintain the separation of allocation and initialization. You must also implement the chaining of initializers that ensures that objects are initialized first as instances of their root class, and successivelyas instances of more derived classes.
To coordinate these steps you are obliged to manage details of object creation that other languages automate. The advantage is that you can more easily understand the behavior of your program by direct inspection.
Your class should always inherit or provide a class method for returning a pointer to cleared memory where the object will be stored. You shouldn’t override this method in subclasses. In addition to reserving memory, +alloc needs to set up the internal structure of an object before it is initialized.
This makes writing a root class difficult, and your class should usuallyinherit its allocator from a root class provided by your development environment.Both Object and NSObject
supplyan +alloc method. If you need to write your own root class, look at Object.m to see how this is done.
When an object is initialized, it should become a valid, consistent instance of its class. For this to happen, all Objective-C classes need to cooperate to ensure several things:
•All the ancestors of a class must initialize an object before the class itself does.
•Ancestor initialization must proceed in order from the root class to the most-derived class.
•All ancestor initialization calls should be usable on a class’s instance. (The difficulty here is guaranteeing that the class’s own initialization will not be skipped.)
Objective-C programmers have adopted the following design patterns to ensure these conditions are always met:
•Your class mayhave several initialization methods;it is conventional to name those methods starting with init .
•The most specialized initializer (usuallythe one with the most parameters) is called the designated initializer and has a special role:all your class’s other initializers should call it.
•Your class should override the parent class’s designated initializer.
•Your designated initializer should call the parent class’s designated initializer.
Customer Reviews
Very good concise reference
Does exactly what it suggests - gives a clear and concise reference to all the important aspects of Objective-C as a superset of C. For anyone with good C knowledge, this combined with the Apple & GNUstep documentation on the web will be all you need to take advantage of this delightful object-oriented paradigm which balances the speed & generality of C with a simple set of extensions which provide very significant power, much closer to the 'true' Smalltalk object model than the theoretically efficient but often infuriatingly obtuse mixed type/generic/object model of C++ which can become a nightmare to develop in.
Superb Reference
No-one likes spending any longer than necessary reading technical books. We read these books because (we hope!) they are condensed instruction manuals, so after reading them, we know what to do, and can get on with it.
This book achieves exactly that: it is compact and concise. It introduces what may be new concepts without any fanfares... it just states - very clearly - what the different parts of the language, and the Objective-C run-time (Cocoa or Cocoa Touch, for the iPhone and iPod touch), do.
You will have to pay attention to the text in this book. There are very few diagrams, (although the examples of interfaces and implementations are neatly explained in Objective-C). If you come from a C, Java or C++ background, most of the verbal descriptions will be easy to comprehend. You may have to re-read some paragraphs, because there is so much information contained in this book, but re-reading should clarify the subject(s) for you. Duncan's writing style is fluid, accurate and not too dry - a welcome relief!
Get this great value book if you need to learn Objective-C and already know C/C++ and don't want a "Dummy-style" explanation.
One of the best O'Reilly books I have ever bought.




