<!--
{
  "availability" : [
    "iOS: 2.0.0 -",
    "iPadOS: 2.0.0 -",
    "macCatalyst: 13.0.0 -",
    "macOS: 10.0.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/NSSecureCoding",
  "metadataVersion" : "0.1.0",
  "role" : "Protocol",
  "symbol" : {
    "kind" : "Protocol",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(pl)NSSecureCoding"
  },
  "title" : "NSSecureCoding"
}
-->

# NSSecureCoding

A protocol that enables encoding and decoding in a manner that is robust against object substitution attacks.

```
protocol NSSecureCoding : NSCoding
```

## Overview

Historically, many classes decoded instances of themselves like this:

```objc
id obj = [decoder decodeObjectForKey:@"myKey"];
if (![obj isKindOfClass:[MyClass class]]) { /* ...fail... */ }
```

This technique is potentially unsafe because by the time you can verify the class type, the object has already been constructed, and if this is part of a collection class, potentially inserted into an object graph.

In order to conform to [`NSSecureCoding`](/documentation/Foundation/NSSecureCoding):

- An object that does not override [`init(coder:)`](/documentation/Foundation/NSCoding/init(coder:)) can conform to `NSSecureCoding` without any changes (assuming that it is a subclass of another class that conforms).
- An object that does override [`init(coder:)`](/documentation/Foundation/NSCoding/init(coder:)) must decode any enclosed objects using the [`decodeObjectOfClass:forKey:`](/documentation/Foundation/NSCoder/decodeObjectOfClass:forKey:) method. For example:

```objc
  id obj = [decoder decodeObjectOfClass:[MyClass class]
              forKey:@"myKey"];
```

In addition, the class must override the getter for its [`supportsSecureCoding`](/documentation/Foundation/NSSecureCoding/supportsSecureCoding) property to return <doc://com.apple.documentation/documentation/Swift/true>.

For more information about how this relates to the NSXPC API, see [Creating XPC Services](https://developer.apple.com/library/archive/documentation/MacOSX/Conceptual/BPSystemStartup/Chapters/CreatingXPCServices.html#//apple_ref/doc/uid/10000172i-SW6) in [Daemons and Services Programming Guide](https://developer.apple.com/library/archive/documentation/MacOSX/Conceptual/BPSystemStartup/Chapters/Introduction.html#//apple_ref/doc/uid/10000172i).

## Topics

### Checking for Secure Coding

[`static var supportsSecureCoding: Bool`](/documentation/Foundation/NSSecureCoding/supportsSecureCoding)

A Boolean value that indicates whether or not the class supports secure coding.

## Relationships

### Conforming Types

[`NSHashTable`](/documentation/Foundation/NSHashTable)

[`UnitSpeed`](/documentation/Foundation/UnitSpeed)

[`NSMutableData`](/documentation/Foundation/NSMutableData)

[`NSLocale`](/documentation/Foundation/NSLocale)

[`NSPredicate`](/documentation/Foundation/NSPredicate)

[`UnitElectricCurrent`](/documentation/Foundation/UnitElectricCurrent)

[`NSNumber`](/documentation/Foundation/NSNumber)

[`NSMutableDictionary`](/documentation/Foundation/NSMutableDictionary)

[`UnitElectricPotentialDifference`](/documentation/Foundation/UnitElectricPotentialDifference)

[`NSMutableIndexSet`](/documentation/Foundation/NSMutableIndexSet)

[`ISO8601DateFormatter`](/documentation/Foundation/ISO8601DateFormatter)

[`URLCredential`](/documentation/Foundation/URLCredential)

[`NSIndexSet`](/documentation/Foundation/NSIndexSet)

[`NSExtensionItem`](/documentation/Foundation/NSExtensionItem)

[`NSDateInterval`](/documentation/Foundation/NSDateInterval)

[`NSIndexPath`](/documentation/Foundation/NSIndexPath)

[`UnitDispersion`](/documentation/Foundation/UnitDispersion)

[`NSPointerArray`](/documentation/Foundation/NSPointerArray)

[`UnitPressure`](/documentation/Foundation/UnitPressure)

[`NSMutableOrderedSet`](/documentation/Foundation/NSMutableOrderedSet)

[`NSOrderedSet`](/documentation/Foundation/NSOrderedSet)

[`NSMutableString`](/documentation/Foundation/NSMutableString)

[`NSTimeZone`](/documentation/Foundation/NSTimeZone)

[`NSURLQueryItem`](/documentation/Foundation/NSURLQueryItem)

[`HTTPURLResponse`](/documentation/Foundation/HTTPURLResponse)

[`NSDataDetector`](/documentation/Foundation/NSDataDetector)

[`NSCharacterSet`](/documentation/Foundation/NSCharacterSet)

[`UnitConverterLinear`](/documentation/Foundation/UnitConverterLinear)

[`Unit`](/documentation/Foundation/Unit)

[`UnitVolume`](/documentation/Foundation/UnitVolume)

[`NSDecimalNumber`](/documentation/Foundation/NSDecimalNumber)

[`NSCompoundPredicate`](/documentation/Foundation/NSCompoundPredicate)

[`URLResponse`](/documentation/Foundation/URLResponse)

[`NSAppleEventDescriptor`](/documentation/Foundation/NSAppleEventDescriptor)

[`NSAffineTransform`](/documentation/Foundation/NSAffineTransform)

[`CachedURLResponse`](/documentation/Foundation/CachedURLResponse)

[`NSURL`](/documentation/Foundation/NSURL)

[`UnitLength`](/documentation/Foundation/UnitLength)

[`NSRegularExpression`](/documentation/Foundation/NSRegularExpression)

[`FileWrapper`](/documentation/Foundation/FileWrapper)

[`NSMutableAttributedString`](/documentation/Foundation/NSMutableAttributedString)

[`NSAttributedString`](/documentation/Foundation/NSAttributedString)

[`NSString`](/documentation/Foundation/NSString)

[`NSArray`](/documentation/Foundation/NSArray)

[`UnitArea`](/documentation/Foundation/UnitArea)

[`NSMutableArray`](/documentation/Foundation/NSMutableArray)

[`UnitMass`](/documentation/Foundation/UnitMass)

[`UnitEnergy`](/documentation/Foundation/UnitEnergy)

[`NSSortDescriptor`](/documentation/Foundation/NSSortDescriptor)

[`NSPersonNameComponents`](/documentation/Foundation/NSPersonNameComponents)

[`MeasurementFormatter`](/documentation/Foundation/MeasurementFormatter)

[`NSCountedSet`](/documentation/Foundation/NSCountedSet)

[`UnitTemperature`](/documentation/Foundation/UnitTemperature)

[`NSMutableURLRequest`](/documentation/Foundation/NSMutableURLRequest)

[`NSOrthography`](/documentation/Foundation/NSOrthography)

[`UnitAcceleration`](/documentation/Foundation/UnitAcceleration)

[`UnitAngle`](/documentation/Foundation/UnitAngle)

[`NSMapTable`](/documentation/Foundation/NSMapTable)

[`NSError`](/documentation/Foundation/NSError)

[`UnitPower`](/documentation/Foundation/UnitPower)

[`UnitInformationStorage`](/documentation/Foundation/UnitInformationStorage)

[`NSData`](/documentation/Foundation/NSData)

[`UnitIlluminance`](/documentation/Foundation/UnitIlluminance)

[`NSFileSecurity`](/documentation/Foundation/NSFileSecurity)

[`UnitFrequency`](/documentation/Foundation/UnitFrequency)

[`UnitFuelEfficiency`](/documentation/Foundation/UnitFuelEfficiency)

[`EnergyKit`](/documentation/Foundation/UnitEnergy/EnergyKit)

[`NSNull`](/documentation/Foundation/NSNull)

[`NSMeasurement`](/documentation/Foundation/NSMeasurement)

[`NSURLRequest`](/documentation/Foundation/NSURLRequest)

[`NSTextCheckingResult`](/documentation/Foundation/NSTextCheckingResult)

[`NSDate`](/documentation/Foundation/NSDate)

[`URLProtectionSpace`](/documentation/Foundation/URLProtectionSpace)

[`NSXPCListenerEndpoint`](/documentation/Foundation/NSXPCListenerEndpoint)

[`NSValue`](/documentation/Foundation/NSValue)

[`UnitConcentrationMass`](/documentation/Foundation/UnitConcentrationMass)

[`NSException`](/documentation/Foundation/NSException)

[`NSMutableCharacterSet`](/documentation/Foundation/NSMutableCharacterSet)

[`NSMutableSet`](/documentation/Foundation/NSMutableSet)

[`UnitElectricCharge`](/documentation/Foundation/UnitElectricCharge)

[`NSComparisonPredicate`](/documentation/Foundation/NSComparisonPredicate)

[`UnitDuration`](/documentation/Foundation/UnitDuration)

[`NSCalendar`](/documentation/Foundation/NSCalendar)

[`URLAuthenticationChallenge`](/documentation/Foundation/URLAuthenticationChallenge)

[`Dimension`](/documentation/Foundation/Dimension)

[`FileHandle`](/documentation/Foundation/FileHandle)

[`UnitElectricResistance`](/documentation/Foundation/UnitElectricResistance)

[`NSSet`](/documentation/Foundation/NSSet)

[`NSDictionary`](/documentation/Foundation/NSDictionary)

[`NSPurgeableData`](/documentation/Foundation/NSPurgeableData)

[`NSUUID`](/documentation/Foundation/NSUUID)

[`NSDateComponents`](/documentation/Foundation/NSDateComponents)

[`NSExpression`](/documentation/Foundation/NSExpression)

### Inherits From

[`NSCoding`](/documentation/Foundation/NSCoding)

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)