<!--
{
  "availability" : [
    "iOS: 17.0.0 -",
    "iPadOS: 17.0.0 -",
    "macCatalyst: 17.0.0 -",
    "macOS: 14.0.0 -",
    "tvOS: 17.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 10.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "SwiftUI",
  "identifier" : "/documentation/SwiftUI/View/environment(_:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "SwiftUI"
    ],
    "preciseIdentifier" : "s:7SwiftUI4ViewPAAE11environmentyQrqd__SgRld__C11Observation10ObservableRd__lF"
  },
  "title" : "environment(_:)"
}
-->

# environment(_:)

Places an observable object in the view’s environment.

```
nonisolated func environment<T>(_ object: T?) -> some View where T : AnyObject, T : Observable
```

## Parameters

`object`

The object to set for this object’s type in the
environment, or `nil` to clear an object of this type from the
environment.

## Return Value

A view that has the specified object in its environment.

## Discussion

Use this modifier to place an object that you declare with the
<doc://com.apple.documentation/documentation/Observation/Observable()>
macro into a view’s environment. For example, you can add an instance
of a custom observable `Profile` class to the environment of a
`ContentView`:

```
@Observable class Profile { ... }

struct RootView: View {
    @State private var currentProfile: Profile?

    var body: some View {
        ContentView()
            .environment(currentProfile)
    }
}
```

You then read the object inside `ContentView` or one of its descendants
using the [`Environment`](/documentation/SwiftUI/Environment) property wrapper:

```
struct ContentView: View {
    @Environment(Profile.self) private var currentProfile: Profile

    var body: some View { ... }
}
```

This modifier affects the given view, as well as that view’s descendant
views. It has no effect outside the view hierarchy on which you call it.
The environment of a given view hierarchy holds only one observable
object of a given type.

> Note: This modifier takes an object that conforms to the
> <doc://com.apple.documentation/documentation/Observation/Observable>
> protocol. To add environment objects that conform to the
> <doc://com.apple.documentation/documentation/Combine/ObservableObject>
> protocol, use ``doc://com.apple.SwiftUI/documentation/SwiftUI/View/environmentObject(_:)`` instead.

---

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)