NSValue Class Reference
| Inherits from | |
| Conforms to | |
| Framework | /System/Library/Frameworks/Foundation.framework |
| Availability | Available in OS X v10.0 and later. |
| Companion guide | |
| Declared in | NSGeometry.h NSRange.h NSValue.h |
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
Tasks
Creating an NSValue
-
– initWithBytes:objCType: -
+ valueWithBytes:objCType: -
+ value:withObjCType: -
+ valueWithNonretainedObject: -
+ valueWithPointer: -
+ valueWithPoint: -
+ valueWithRange: -
+ valueWithRect: -
+ valueWithSize:
Accessing Data
-
– getValue: -
– nonretainedObjectValue -
– objCType -
– pointValue -
– pointerValue -
– rangeValue -
– rectValue -
– sizeValue
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.
Parameters
- value
The value for the new
NSValueobject.- 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 OS X v10.0 and later.
See Also
Declared In
NSValue.hvalueWithBytes:objCType:
Creates and returns an NSValue object that contains a given value, which is interpreted as being of a given Objective-C type.
Parameters
- value
The value for the new
NSValueobject.- 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 OS X v10.0 and later.
See Also
Declared In
NSValue.hvalueWithNonretainedObject:
Creates and returns an NSValue object that contains a given object.
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 OS X v10.0 and later.
See Also
Declared In
NSValue.hvalueWithPoint:
Creates and returns an NSValue object that contains a given NSPoint structure.
Parameters
- aPoint
The value for the new object.
Return Value
A new NSValue object that contains the value of point.
Availability
- Available in OS X v10.0 and later.
See Also
Declared In
NSGeometry.hvalueWithPointer:
Creates and returns an NSValue object that contains a given pointer.
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 OS X v10.0 and later.
See Also
Declared In
NSValue.hvalueWithRange:
Creates and returns an NSValue object that contains a given NSRange structure.
Parameters
- range
The value for the new object.
Return Value
A new NSValue object that contains the value of range.
Availability
- Available in OS X v10.0 and later.
See Also
Declared In
NSRange.hvalueWithRect:
Creates and returns an NSValue object that contains a given NSRect structure.
Parameters
- rect
The value for the new object.
Return Value
A new NSValue object that contains the value of rect.
Availability
- Available in OS X v10.0 and later.
See Also
Declared In
NSGeometry.hvalueWithSize:
Creates and returns an NSValue object that contains a given NSSize structure.
Parameters
- size
The value for the new object.
Return Value
A new NSValue object that contains the value of size.
Availability
- Available in OS X v10.0 and later.
See Also
Declared In
NSGeometry.hInstance Methods
getValue:
Copies the receiver’s value into a given 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 OS X v10.0 and later.
Declared In
NSValue.hinitWithBytes:objCType:
Initializes and returns an NSValue object that contains a given value, which is interpreted as being of a given Objective-C type.
Parameters
- value
The value for the new
NSValueobject.- 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 OS X v10.0 and later.
Declared In
NSValue.hisEqualToValue:
Returns a Boolean value that indicates whether the receiver and another value are equal.
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 OS X v10.0 and later.
Declared In
NSValue.hnonretainedObjectValue
Returns the receiver's value as an id.
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 OS X v10.0 and later.
See Also
Declared In
NSValue.hobjCType
Returns a C string containing the Objective-C type of the data contained in the receiver.
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 OS X v10.0 and later.
Declared In
NSValue.hpointerValue
Returns the receiver's value as a pointer to void.
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 OS X v10.0 and later.
See Also
Declared In
NSValue.hpointValue
Returns an NSPoint structure representation of the receiver.
Return Value
An NSPoint structure representation of the receiver.
Availability
- Available in OS X v10.0 and later.
See Also
Declared In
NSGeometry.hrangeValue
Returns an NSRange structure representation of the receiver.
Return Value
An NSRange structure representation of the receiver.
Availability
- Available in OS X v10.0 and later.
See Also
Declared In
NSRange.hrectValue
Returns an NSRect structure representation of the receiver.
Return Value
An NSRect structure representation of the receiver.
Availability
- Available in OS X v10.0 and later.
See Also
Declared In
NSGeometry.hsizeValue
Returns an NSSize structure representation of the receiver.
Return Value
An NSSize structure representation of the receiver.
Availability
- Available in OS X v10.0 and later.
See Also
Declared In
NSGeometry.h© 2007 Apple Inc. All Rights Reserved. (Last updated: 2007-10-31)