<!--
{
  "availability" : [
    "iOS: 18.0.0 -",
    "iPadOS: 18.0.0 -",
    "macCatalyst: 18.0.0 -",
    "macOS: 15.0.0 -",
    "tvOS: 18.0.0 -",
    "visionOS: 2.0.0 -",
    "watchOS: 11.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/NSKeyValueSharedObservers/addSharedObserver(_:forKey:options:context:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSKeyValueSharedObservers(im)addSharedObserver:forKey:options:context:"
  },
  "title" : "addSharedObserver(_:forKey:options:context:)"
}
-->

# addSharedObserver(_:forKey:options:context:)

Add a new observer to the collection.

```
func addSharedObserver(_ observer: NSObject, forKey key: String, options: NSKeyValueObservingOptions = [], context: UnsafeMutableRawPointer?)
```

## Parameters

`observer`

The observer object to register for KVO notifications.
The observer must implement the key-value observing method `observeValue: forKeyPath: of: change: context:`

`key`

key of the property being observed. This cannot be a nested
key path or a computed property

`options`

A combination of NSKeyValueObservingOptions values that
specify what is included in observation notifications. For possible values
see NSKeyValueObservingOptions.

`context`

Arbitrary data which is passed to the observer object

## Discussion

This method works like `-[NSObject addObserver: forKey: options: context:]`,
but observations on nested and computed properties are disallowed. Observers
are not registered until `setSharedObservers` is called on the observable.

---

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)