NSValue Class Reference

Inherits from
Conforms to
Framework
/System/Library/Frameworks/Foundation.framework
Availability
Available in iOS 2.0 and later.
Companion guide
Declared in
NSRange.h
NSValue.h
Related sample code

Overview

An NSValue object is a simple container for a single C or Objective-C data item. It can hold any of the scalar types such as int, float, and char, as well as pointers, structures, and object ids. The purpose of this class is to allow items of such data types to be added to collections such as instances of NSArray and NSSet, which require their elements to be objects. NSValue objects are always immutable.

Adopted Protocols

NSCoding
NSCopying

Tasks

Creating an NSValue

Accessing Data

Comparing Objects

Class Methods

value:withObjCType:

Creates and returns an NSValue object that contains a given value which is interpreted as being of a given Objective-C type.

+ (NSValue *)value:(const void *)value withObjCType:(const char *)type
Parameters
value

The value for the new NSValue object.

type

The Objective-C type of value. type should be created with the Objective-C @encode() compiler directive; it should not be hard-coded as a C string.

Return Value

A new NSValue object that contains value, which is interpreted as being of the Objective-C type type.

Discussion

This method has the same effect as valueWithBytes:objCType: and may be deprecated in a future release. You should use valueWithBytes:objCType: instead.

Availability
  • Available in iOS 2.0 and later.
Declared In
NSValue.h

valueWithBytes:objCType:

Creates and returns an NSValue object that contains a given value, which is interpreted as being of a given Objective-C type.

+ (NSValue *)valueWithBytes:(const void *)value objCType:(const char *)type
Parameters
value

The value for the new NSValue object.

type

The Objective-C type of value. type should be created with the Objective-C @encode() compiler directive; it should not be hard-coded as a C string.

Return Value

A new NSValue object that contains value, which is interpreted as being of the Objective-C type type.

Discussion

See Number and Value Programming Topics for other considerations in creating an NSValue object and code examples.

Availability
  • Available in iOS 2.0 and later.
Declared In
NSValue.h

valueWithNonretainedObject:

Creates and returns an NSValue object that contains a given object.

+ (NSValue *)valueWithNonretainedObject:(id)anObject
Parameters
anObject

The value for the new object.

Return Value

A new NSValue object that contains anObject.

Discussion

This method is equivalent to invoking value:withObjCType: in this manner:

NSValue *theValue = [NSValue value:&anObject withObjCType:@encode(void *)];

This method is useful if you want to add an object to a collection but don’t want the collection to create a strong reference to it.

Availability
  • Available in iOS 2.0 and later.
Declared In
NSValue.h

valueWithPointer:

Creates and returns an NSValue object that contains a given pointer.

+ (NSValue *)valueWithPointer:(const void *)aPointer
Parameters
aPointer

The value for the new object.

Return Value

A new NSValue object that contains aPointer.

Discussion

This method is equivalent to invoking value:withObjCType: in this manner:

NSValue *theValue = [NSValue value:&aPointer withObjCType:@encode(void *)];

This method does not copy the contents of aPointer, so you must not to free the memory at the pointer destination while the NSValue object exists. NSData objects may be more suited for arbitrary pointers than NSValue objects.

Availability
  • Available in iOS 2.0 and later.
Declared In
NSValue.h

valueWithRange:

Creates and returns an NSValue object that contains a given NSRange structure.

+ (NSValue *)valueWithRange:(NSRange)range
Parameters
range

The value for the new object.

Return Value

A new NSValue object that contains the value of range.

Availability
  • Available in iOS 2.0 and later.
Declared In
NSRange.h

Instance Methods

getValue:

Copies the receiver’s value into a given buffer.

- (void)getValue:(void *)buffer
Parameters
buffer

A buffer into which to copy the receiver's value. buffer must be large enough to hold the value.

Availability
  • Available in iOS 2.0 and later.
Declared In
NSValue.h

initWithBytes:objCType:

Initializes and returns an NSValue object that contains a given value, which is interpreted as being of a given Objective-C type.

- (id)initWithBytes:(const void *)value objCType:(const char *)type
Parameters
value

The value for the new NSValue object.

type

The Objective-C type of value. type should be created with the Objective-C @encode() compiler directive; it should not be hard-coded as a C string.

Return Value

An initialized NSValue object that contains value, which is interpreted as being of the Objective-C type type. The returned object might be different than the original receiver.

Discussion

See Number and Value Programming Topics for other considerations in creating an NSValue object.

This is the designated initializer for the NSValue class.

Availability
  • Available in iOS 2.0 and later.
Declared In
NSValue.h

isEqualToValue:

Returns a Boolean value that indicates whether the receiver and another value are equal.

- (BOOL)isEqualToValue:(NSValue *)value
Parameters
aValue

The value with which to compare the receiver.

Return Value

YES if the receiver and aValue are equal, otherwise NO. For NSValue objects, the class, type, and contents are compared to determine equality.

Availability
  • Available in iOS 2.0 and later.
Declared In
NSValue.h

nonretainedObjectValue

Returns the receiver's value as an id.

- (id)nonretainedObjectValue
Return Value

The receiver's value as an id. If the receiver was not created to hold a pointer-sized data item, the result is undefined.

Availability
  • Available in iOS 2.0 and later.
Declared In
NSValue.h

objCType

Returns a C string containing the Objective-C type of the data contained in the receiver.

- (const char *)objCType
Return Value

A C string containing the Objective-C type of the data contained in the receiver, as encoded by the @encode() compiler directive.

Availability
  • Available in iOS 2.0 and later.
Declared In
NSValue.h

pointerValue

Returns the receiver's value as a pointer to void.

- (void *)pointerValue
Return Value

The receiver's value as a pointer to void. If the receiver was not created to hold a pointer-sized data item, the result is undefined.

Availability
  • Available in iOS 2.0 and later.
Declared In
NSValue.h

rangeValue

Returns an NSRange structure representation of the receiver.

- (NSRange)rangeValue
Return Value

An NSRange structure representation of the receiver.

Availability
  • Available in iOS 2.0 and later.
Declared In
NSRange.h