<!--
{
  "availability" : [
    "iOS: 14.0.0 -",
    "iPadOS: 14.0.0 -",
    "macCatalyst: 14.0.0 -",
    "macOS: 11.0.0 -",
    "tvOS: 14.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 7.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Combine",
  "identifier" : "/documentation/Combine/Publisher/assign(to:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Combine"
    ],
    "preciseIdentifier" : "s:7Combine9PublisherPAAs5NeverO7FailureRtzrlE6assign2toyAA9PublishedVABVy6OutputQz_Gz_tF"
  },
  "title" : "assign(to:)"
}
-->

# assign(to:)

Republishes elements received from a publisher, by assigning them to a property marked as a publisher.

```
func assign(to published: inout Published<Self.Output>.Publisher)
```

## Parameters

`published`

A property marked with the `@Published` attribute, which receives and republishes all elements received from the upstream publisher.

## Discussion

Use this operator when you want to receive elements from a publisher and republish them through a property marked with the `@Published` attribute. The `assign(to:)` operator manages the life cycle of the subscription, canceling the subscription automatically when the [`Published`](/documentation/Combine/Published) instance deinitializes. Because of this, the `assign(to:)` operator doesn’t return an [`AnyCancellable`](/documentation/Combine/AnyCancellable) that you’re responsible for like [`assign(to:on:)`](/documentation/Combine/Publisher/assign(to:on:)) does.

The example below shows a model class that receives elements from an internal <doc://com.apple.documentation/documentation/Foundation/Timer/TimerPublisher>, and assigns them to a `@Published` property called `lastUpdated`. Because the `to` parameter has the `inout` keyword, you need to use the `&` operator when calling this method.

```
class MyModel: ObservableObject {
    @Published var lastUpdated: Date = Date()
    init() {
         Timer.publish(every: 1.0, on: .main, in: .common)
             .autoconnect()
             .assign(to: &$lastUpdated)
    }
}
```

If you instead implemented `MyModel` with `assign(to: lastUpdated, on: self)`, storing the returned [`AnyCancellable`](/documentation/Combine/AnyCancellable) instance could cause a reference cycle, because the [`Subscribers.Assign`](/documentation/Combine/Subscribers/Assign) subscriber would hold a strong reference to `self`. Using `assign(to:)` solves this problem.

While the `to` parameter uses the `inout` keyword, this method doesn’t replace a reference type passed to it. Instead, this notation indicates that the operator may modify members of the assigned object, as seen in the following example:

```
    class MyModel2: ObservableObject {
        @Published var id: Int = 0
    }
    let model2 = MyModel2()
    Just(100).assign(to: &model2.$id)
```

---

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)