Cover | Table of Contents | Colophon
NSObject
NSString, supplants the familiar C
programming data type char * to represent
character string data. String objects contain Unicode characters
rather than the narrow range of characters afforded by the ASCII
character set, allowing them to contain characters in any language,
including Chinese, Arabic, and Hebrew. The string classes provide an
API to create both mutable and immutable strings and to perform
string operations such as substring searching, string comparison, and
concatenation.
|
Name
|
Description
|
|---|---|
|
FileMerge
|
Visually compares the contents of two files or two directories. You
can use FileMerge to determine the differences between versions of
the same source-code file or between two project directories. You can
also use it to merge changes.
|
|
icns Browser
|
Displays the entire contents of Mac OS X icon files.
|
|
IconComposer
|
Creates Mac OS X icons from source art.
|
|
IORegistryExplorer
|
Provides a hierarchical display of the system I/O registry.
|
|
MallocDebug
|
Measures the dynamic-memory usage of applications, finds memory
leaks, analyzes all allocated memory in an application, and measures
the memory allocated since a given time.
|
|
ObjectAlloc
|
Tracks and displays all Cocoa and Core Foundation object allocations
for a running application. ObjectAlloc allows you to view the list of
objects, as well as the call stack that resulted in each allocation.
|
http://developer.apple.com/techpubs/macosx/DeveloperTools/ProjectBuilderAccess/index.html.
int main (int argc, const char * argv[]) {
NSAutoReleasepool *pool = [[NSAutoreleasepool alloc] init];
NSObject * object; // a
object = [NSObject alloc]; // b
object = [object init]; // c
NSLog(@"Created object: %@", object); // d
[pool release];
return 0;
}
object of type
NSObject. You should recognize this as a regular C
pointer.
NSObject and assigns
it to the object variable. The
alloc method reserves (or allocates) memory space
for the object and returns a pointer to that space.
We'll explain more about methods in just a bit.
init call initializes the object so it can be
used. The initalloc method is an example of a class method.
Instance methods, on the other hand, are scoped to object instances.
The init
method is an example of an instance
method that is called on an instance of an object returned by the
alloc method.
NSObject class, and the
method to be called is the alloc method. In
response to receiving this message, the NSObject
class returns a new instance of the class that will be assigned to
the variable anObject.
SEL. Before we continue, Table 3-1 lists the set of
Objective-C-defined types.
|
Type
|
Definition
|
|---|---|
id |
An object reference (a pointer to its data structure)
|
Class |
A class object reference (a pointer to its data structure)
|
SEL |
A selector (a compiler-assigned code that identifies a method name)
|
IMP |
A pointer to a method implementation that returns an id
|
BOOL |
A Boolean value, either
YES or
NO
|
nil |
A null object pointer,
(id)0
|
Nil |
A null class pointer,
(Class)0
|
id type can be used to type any kind of
object, class, or instance. In addition, class names can be used as
type names to type instances of a class statically. A statically
typed instance is declared as a pointer to an instance of its class
or to an instance of any class from which it inherits.
description
method.
#import "Song.h"
@implementation Song
- (NSString *)name
{
return name;
}
- (void)setName:(NSString *)newName
{
[newName retain];
[name release];
name = newName;
}
- (NSString *)artist
{
return artist;
}
- (void)setArtist:(NSString *)newArtist
{
[newArtist retain];
[artist release];
artist = newArtist;
}
- (NSString *)description // a
{
return [self name]; // b
}
@end
description method that overrides the
method by the same name in the NSObject class. We
don't need to declare this method in the
Song.h interface file, as it is already part of
the interface declared by NSObject.
id, can reveal its class and
divulge other characteristics at runtime. Several introspection
methods, such as isMemberOfClass: and
isKindOfClass:, allow you to ascertain the
inheritance relationships of an object and the methods to which it
responds.
NSObject and
NSString.
NSLog function.isa and self
variables by having the designated initializer of the Song class
print a description of the class.
@" . . .
"
construct in various method and function calls. This construct is
convenient when working with strings. When interpreted by the
compiler, it is translated into an NSString object
that is based on the 7-bit ASCII-encoded string (also known as a
"C string") between the quotes. For
example, the statement:
NSString * lush = @"Lush";
NSString * lush = [[NSString alloc] initWithCString:"Lush"];
NSString objects are not limited to the ASCII
character set; they can handle any character contained in the Unicode
character set, allowing most of the world's living
languages to be represented. Unicode is a 16-bit-wide character set,
but can be represented in 8-bits using the UTF-8 encoding.
NSString
provides several methods that are handy
when working with strings. A few of these methods are as follows:
- (int)length
@" . . .
"
construct in various method and function calls. This construct is
convenient when working with strings. When interpreted by the
compiler, it is translated into an NSString object
that is based on the 7-bit ASCII-encoded string (also known as a
"C string") between the quotes. For
example, the statement:
NSString * lush = @"Lush";
NSString * lush = [[NSString alloc] initWithCString:"Lush"];
NSString objects are not limited to the ASCII
character set; they can handle any character contained in the Unicode
character set, allowing most of the world's living
languages to be represented. Unicode is a 16-bit-wide character set,
but can be represented in 8-bits using the UTF-8 encoding.
NSString
provides several methods that are handy
when working with strings. A few of these methods are as follows:
- (int)length
- (const char *)cString
NSString class can handle the full
Unicode character set, not all NSString objects
can be represented as C strings.
NSArray
class—are ordered collections of
objects indexed by integers. Like C-based arrays, the first object in
an array is located at index 0. Unlike C- and Java-based arrays whose
size is set when they are created, Cocoa mutable array objects can
grow as needed to accommodate inserted objects.