---
title: NSNumber
framework: foundation
role: symbol
role_heading: Class
path: foundation/nsnumber
---

# NSNumber

An object wrapper for primitive scalar numeric values.

## Declaration

```swift
class NSNumber
```

## Mentioned in

Implementing Handoff in Your App

## Overview

Overview NSNumber is a subclass of NSValue that offers a value as any C scalar (numeric) type. It defines a set of methods specifically for setting and accessing the value as a signed or unsigned char, short int, int, long int, long long int, float, or double or as a BOOL. (Note that number objects do not necessarily preserve the type they are created with.) It also defines a compare(_:) method to determine the ordering of two NSNumber objects. NSNumber is “toll-free bridged” with its Core Foundation counterparts: CFNumber for integer and floating point values, and CFBoolean for Boolean values. See Toll-Free Bridging for more information on toll-free bridging. Value Conversions NSNumber provides readonly properties that return the object’s stored value converted to a particular Boolean, integer, unsigned integer, or floating point C scalar type. Because numeric types have different storage capabilities, attempting to initialize with a value of one type and access the value of another type may produce an erroneous result—for example, initializing with a double value exceeding FLT_MAX and accessing its floatValue, or initializing with an negative integer value and accessing its uintValue. In some cases, attempting to initialize with a value of a type and access the value of another type may result in loss of precision—for example, initializing with a double value with many significant digits and accessing its floatValue, or initializing with a large integer value and accessing its int8Value. An NSNumber object initialized with a value of a particular type accessing the converted value of a different kind of type, such as unsigned int and float, will convert its stored value to that converted type in the following ways:  |  |  |  |   |  |  |  |   |  |  |  |   |  |  |  |   |  |  |  |   |  |  |  |   |  |  |  |   |  |  |  |   |  |  |  |   |  |  |  |   |  |  |  |   |  |  |  |   |  |  |  |   |  |  |  |  Subclassing Notes As with any class cluster, subclasses of NSNumber must override the primitive methods of its superclass, NSValue. In addition, there are two requirements around the data type your subclass represents: Your implementation of objCType must return one of “c”, “C”, “s”, “S”, “i”, “I”, “l”, “L”, “q”, “Q”, “f”, and “d”. This is required for the other methods of NSNumber to behave correctly. Your subclass must override the accessor method that corresponds to the declared type—for example, if your implementation of objCType returns  “i”, you must override int32Value.

## Topics

### Initializing an NSNumber Object

- [init(value:)](foundation/nsnumber/init(value:)-1ojz2.md)
- [init(value:)](foundation/nsnumber/init(value:)-8krjs.md)
- [init(value:)](foundation/nsnumber/init(value:)-15chk.md)
- [init(value:)](foundation/nsnumber/init(value:)-2vlwk.md)
- [init(value:)](foundation/nsnumber/init(value:)-7jvmg.md)
- [init(value:)](foundation/nsnumber/init(value:)-5jcjl.md)
- [init(value:)](foundation/nsnumber/init(value:)-40ad0.md)
- [init(value:)](foundation/nsnumber/init(value:)-16drx.md)
- [init(value:)](foundation/nsnumber/init(value:)-8se67.md)
- [init(value:)](foundation/nsnumber/init(value:)-47coa.md)
- [init(value:)](foundation/nsnumber/init(value:)-3l4ek.md)
- [init(value:)](foundation/nsnumber/init(value:)-43lc7.md)
- [init(value:)](foundation/nsnumber/init(value:)-87y9m.md)

### Accessing Numeric Values

- [boolValue](foundation/nsnumber/boolvalue.md)
- [int8Value](foundation/nsnumber/int8value.md)
- [decimalValue](foundation/nsnumber/decimalvalue.md)
- [doubleValue](foundation/nsnumber/doublevalue.md)
- [floatValue](foundation/nsnumber/floatvalue.md)
- [int32Value](foundation/nsnumber/int32value.md)
- [intValue](foundation/nsnumber/intvalue-95zzp.md)
- [int64Value](foundation/nsnumber/int64value.md)
- [int16Value](foundation/nsnumber/int16value.md)
- [uint8Value](foundation/nsnumber/uint8value.md)
- [uintValue](foundation/nsnumber/uintvalue.md)
- [uint32Value](foundation/nsnumber/uint32value.md)
- [uint64Value](foundation/nsnumber/uint64value.md)
- [uint16Value](foundation/nsnumber/uint16value.md)

### Retrieving String Representations

- [description(withLocale:)](foundation/nsnumber/description(withlocale:).md)
- [stringValue](foundation/nsnumber/stringvalue.md)

### Comparing NSNumber Objects

- [compare(_:)](foundation/nsnumber/compare(_:).md)
- [isEqual(to:)](foundation/nsnumber/isequal(to:).md)

### Number Validation

- [NSDecimalIsNotANumber(_:)](foundation/nsdecimalisnotanumber(_:).md)

### Initializers

- [init(bool:)](foundation/nsnumber/init(bool:).md)
- [init(char:)](foundation/nsnumber/init(char:).md)
- [init(coder:)](foundation/nsnumber/init(coder:).md)
- [init(double:)](foundation/nsnumber/init(double:).md)
- [init(float:)](foundation/nsnumber/init(float:).md)
- [init(int:)](foundation/nsnumber/init(int:).md)
- [init(integer:)](foundation/nsnumber/init(integer:).md)
- [init(longLong:)](foundation/nsnumber/init(longlong:).md)
- [init(short:)](foundation/nsnumber/init(short:).md)
- [init(unsignedChar:)](foundation/nsnumber/init(unsignedchar:).md)
- [init(unsignedInt:)](foundation/nsnumber/init(unsignedint:).md)
- [init(unsignedInteger:)](foundation/nsnumber/init(unsignedinteger:).md)
- [init(unsignedLongLong:)](foundation/nsnumber/init(unsignedlonglong:).md)
- [init(unsignedShort:)](foundation/nsnumber/init(unsignedshort:).md)

### Default Implementations

- [ExpressibleByBooleanLiteral Implementations](foundation/nsnumber/expressiblebybooleanliteral-implementations.md)
- [ExpressibleByFloatLiteral Implementations](foundation/nsnumber/expressiblebyfloatliteral-implementations.md)
- [ExpressibleByIntegerLiteral Implementations](foundation/nsnumber/expressiblebyintegerliteral-implementations.md)

## Relationships

### Inherits From

- [NSValue](foundation/nsvalue.md)

### Inherited By

- [NSDecimalNumber](foundation/nsdecimalnumber.md)

### Conforms To

- [CKRecordValue](cloudkit/ckrecordvalue-c.protocol.md)
- [CKRecordValueProtocol](cloudkit/ckrecordvalueprotocol.md)
- [CVarArg](swift/cvararg.md)
- [Copyable](swift/copyable.md)
- [CustomDebugStringConvertible](swift/customdebugstringconvertible.md)
- [CustomStringConvertible](swift/customstringconvertible.md)
- [Equatable](swift/equatable.md)
- [Escapable](swift/escapable.md)
- [ExpressibleByBooleanLiteral](swift/expressiblebybooleanliteral.md)
- [ExpressibleByFloatLiteral](swift/expressiblebyfloatliteral.md)
- [ExpressibleByIntegerLiteral](swift/expressiblebyintegerliteral.md)
- [Hashable](swift/hashable.md)
- [NSCoding](foundation/nscoding.md)
- [NSCopying](foundation/nscopying.md)
- [NSFetchRequestResult](coredata/nsfetchrequestresult.md)
- [NSObjectProtocol](objectivec/nsobjectprotocol.md)
- [NSSecureCoding](foundation/nssecurecoding.md)
- [Sendable](swift/sendable.md)
- [SendableMetatype](swift/sendablemetatype.md)
