<!--
{
  "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" : "Swift",
  "identifier" : "/documentation/Synchronization/AtomicLazyReference/storeIfNil(_:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Synchronization"
    ],
    "preciseIdentifier" : "s:15Synchronization19AtomicLazyReferenceV10storeIfNilyxxnF"
  },
  "title" : "storeIfNil(_:)"
}
-->

# storeIfNil(_:)

Atomically initializes this reference if its current value is nil, then
returns the initialized value. If this reference is already initialized,
then `storeIfNil(_:)` discards its supplied argument and returns the
current value without updating it.

```
func storeIfNil(_ desired: consuming Instance) -> Instance
```

## Parameters

`desired`

A value of `Instance` that we will attempt to store
if the lazy reference is currently nil.

## Return Value

The value of `Instance` that was successfully stored within the
lazy reference. This may or may not be the same value of `Instance` that
was passed to this function.

## Discussion

The following example demonstrates how this can be used to implement a
thread-safe lazily initialized reference:

```
class Image {
  let _histogram = AtomicLazyReference<Histogram>()

  // This is safe to call concurrently from multiple threads.
  var atomicLazyHistogram: Histogram {
    if let histogram = _histogram.load() { return histogram }
    // Note that code here may run concurrently on
    // multiple threads, but only one of them will get to
    // succeed setting the reference.
    let histogram = ...
    return _histogram.storeIfNil(histogram)
  }
}
```

> Note: This operation uses acquiring-and-releasing memory ordering.

---

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)