Next Page > Hide TOC

Legacy Documentclose button

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

NSRange

Inherits from
Object
Implements
Cloneable
Package
com.apple.cocoa.foundation
Companion guide
Geometry and Range Utilities

Overview

An NSRange represents a range, a measurement of a segment of something linear, such as a byte stream. An NSRange has two primary values, a location and a length. The methods of NSRange give access to these values; convert between NSRanges and their string representations; test and compare ranges; and create ranges based on operations involving the union, intersection, and subtraction of two ranges.

Tasks

Constructors

Accessing Range Elements

Manipulating Ranges

Testing Ranges

Converting Between Strings and NSRanges

Constructors

NSRange

public NSRange()

Discussion

Returns an empty NSRange.

public NSRange(int location, int length)

Discussion

Initializes the NSRange with the range elements of location and length; it throws an IllegalArgumentException if either integer is negative.

public NSRange(NSRange aRange)

Discussion

Initializes the new NSRange with the location and length values of aRange; this constructor is used in cloning the receiver.

Static Methods

fromString

public static NSRange fromString(String rangeAsString)

Discussion

Creates an NSRange from the string rangeAsString, which must be of the form “{loc, len}” where loc is a number representing the starting location of the range and len is the range’s length. Throws an IllegalArgumentException if the string is improperly formatted.

See Also

Instance Methods

clone

Creates and returns a copy of the receiver.

public Object clone()

equals

Returns whether otherObject is an NSRange and is equal in location and length to the receiver.

public boolean equals(Object otherObject)

See Also

hashCode

Provides an appropriate hash code useful for storing the receiver in a hash-based data structure.

public int hashCode()

Discussion

This value is the sum of the receiver’s location and length.

intersectsRange

Returns whether the range aRange intersects the receiver.

public boolean intersectsRange(NSRange aRange)

See Also

isEmpty

Returns whether the length of the receiver is 0.

public boolean isEmpty()

See Also

isEqualToRange

Returns whether the range aRange is equal in both location and length to the receiver.

public boolean isEqualToRange(NSRange aRange)

See Also

isSubrangeOfRange

Returns whether the receiver’s endpoints match or fall within those of range aRange.

public boolean isSubrangeOfRange(NSRange aRange)

See Also

length

Returns the length of the receiver from its starting location.

public int length()

See Also

location

Returns the starting location of the receiver.

public int location()

See Also

locationInRange

Returns whether the location aLocation comes after or matches the starting location and comes before the ending location of the receiver.

public boolean locationInRange(int aLocation)

See Also

maxRange

Returns the extent of the receiver (its starting location plus its length).

public int maxRange()

Discussion

This number is 1 greater than the last location in the range.

See Also

rangeByIntersectingRange

Returns an NSRange that is the intersection of aRange and the receiver.

public NSRange rangeByIntersectingRange(NSRange aRange)

Discussion

If the ranges do not intersect, returns an empty range (see isEmpty).

See Also

rangeByUnioningRange

Returns an NSRange that is the union of aRange and the receiver (a range constructed from the lowest starting location and the highest ending location of both NSRanges).

public NSRange rangeByUnioningRange(NSRange aRange)

See Also

subtractRange

Returns the ranges resulting from the subtraction of otherRange from the receiver by modifying the mutable ranges resultRange1 and resultRange2 (provided by the caller).

public void subtractRange(NSRange otherRange, NSMutableRange resultRange1, NSMutableRange resultRange2)

Discussion

Either or both of the result ranges might be empty when this method returns.

See Also

toString

Returns a String representing the receiver’s starting location and length.

public String toString()

Discussion

The string has the form “{loc, len}”, where loc is the starting location of the range and len is its length.

See Also

Constants

NSRange provides the following constant as a convenience; you can use it to compare values returned by some NSRange methods:

Constant

Description

ZeroRange

An NSRange set to 0 in location and length



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.