Next Page > Hide TOC

Legacy Documentclose button

Important: The information in this document is obsolete and should not be used for new development.

NSObject

Inherits from
Object
Implements
Cloneable
NSKeyValueCoding
java.io.Serializable
Package
com.apple.cocoa.foundation
Companion guide

Overview

NSObject is the root class of most Objective-C class hierarchies. NSObject, along with java.lang.Object, is the root class for all things Cocoa in Java.

Interfaces Implemented

NSKeyValueCoding

Tasks

Constructors

Creating, Copying, and Deallocating Objects

Comparing Objects

Describing Objects

Key Value Coding

Constructors

NSObject

Creates an NSObject. You should create instances of concrete subclasses instead of NSObject.

public NSObject()

Instance Methods

clone

Returns a new instance that’s a copy of the receiver.

public Object clone()

Discussion

The copy returned is immutable if applicable to the receiver; otherwise the exact nature of the copy is determined by the class. Throws java.lang.CloneNotSupportedException.

equals

Returns true if the receiver and anObject are equal, false otherwise.

public boolean equals(Object anObject)

Discussion

NSObject’s implementation compares the id of anObject and the receiver to determine equality. Subclasses can override this method to redefine what it means for objects to be equal. For example, a container object might define two containers as equal if they contain the same contents. See the NSData, NSDictionary, NSArray, and java.lang.String class specifications for examples of the use of this method. If subclasses override equals, they must also override hashCode to ensure that all objects that return true for equals also return the same value for hashCode. Note that equality as defined by this method is not necessarily reflexive. For example, A is equal to B does not imply B is equal to A, especially if B is a subclass of A.

hashCode

Returns an integer that can be used as a table address in a hash table structure.

public int hashCode()

Discussion

NSObject’s implementation returns a value based on the object’s id. If two objects are equal (as determined by the equals method), they must return the same hash value. This last point is particularly important if you define hashCode in a subclass and intend to put instances of that subclass into a collection.

mutableClone

Returns a new instance that’s a mutable copy of the receiver. The copy returned is mutable whether the original is mutable or not.

public Object mutableClone()

takeValueForKey

Sets the value for the property identified by key to value.

public void takeValueForKey(Object value, String key)

Discussion

The default implementation works as follows:

  1. Searches for a public accessor method of the form setKey, invoking it if there is one.

  2. If a public accessor method isn’t found, searches for a private accessor method of the form _setKey, invoking it if there is one.

  3. If an accessor method isn’t found, takeValueForKey searches for an instance variable based on key and sets the value directly. If the key is “lastName”, it searches for an instance variable named _lastName or lastName.

toString

Returns a string that represents the contents of the receiving class.

public String toString()

Discussion

NSObject’s implementation of this method simply prints the name of the class.

valueForKey

Returns the value for the property identified by key.

public Object valueForKey(String key)

Discussion

The default implementation works as follows:

  1. Searches for a public accessor method based on key. For example, with a key of “lastName”, valueForKey looks for a method named getLastName or lastName.

  2. If a public accessor method isn’t found, searches for a private accessor method based on key (a method preceded by an underbar). For example, with a key of “lastName”, valueForKey looks for a method named _getLastName or _lastName.

  3. If an accessor method isn’t found, valueForKey searches for an instance variable based on key and returns its value directly. For the key “lastName”, this would be _lastName or lastName.



Next Page > Hide TOC


© 1997, 2006 Apple Computer, Inc. All Rights Reserved. (Last updated: 2006-07-24)


Did this document help you?
Yes: Tell us what works for you.
It’s good, but: Report typos, inaccuracies, and so forth.
It wasn’t helpful: Tell us what would have helped.