<!--
{
  "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/NSSet/init(set:copyItems:)",
  "metadataVersion" : "0.1.0",
  "role" : "Initializer",
  "symbol" : {
    "kind" : "Initializer",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSSet(im)initWithSet:copyItems:"
  },
  "title" : "init(set:copyItems:)"
}
-->

# init(set:copyItems:)

Initializes a newly allocated set and adds to it members of another given set.

```
convenience init(set: Set<AnyHashable>, copyItems flag: Bool)
```

## Parameters

`set`

A set containing objects to add to the new set.

`flag`

If <doc://com.apple.documentation/documentation/Swift/true>, each object in `set` receives a <doc://com.apple.documentation/documentation/ObjectiveC/NSObject-swift.class/copyWithZone:> message to create a copy of the object—objects must conform to the `NSCopying` protocol. In a managed memory environment, this is instead of the `retain` message the object would otherwise receive. The object copy is then added to the returned set.

    If     <doc://com.apple.documentation/documentation/Swift/false>    , then in a managed memory environment each object in     `set`     simply receives a     `retain`     message when it is added to the returned set.

## Return Value

An initialized set that contains the members of `set`. The returned set might be different than the original receiver.

## Discussion

After an immutable s has been initialized in this way, it cannot be modified.

The [`copy(with:)`](/documentation/Foundation/NSCopying/copy(with:)) method performs a shallow copy. If you have a collection of arbitrary depth, passing <doc://com.apple.documentation/documentation/Swift/true> for the `flag` parameter will perform an immutable copy of the first level below the surface. If you pass <doc://com.apple.documentation/documentation/Swift/false> the mutability of the first level is unaffected. In either case, the mutability of all deeper levels is unaffected.

## See Also

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

Initializes a newly allocated set with members taken from the specified list of objects.

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

Creates and returns a set containing the objects from another set.



---

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)