Nastya Gorban
Nastya Gorban

Reputation: 1241

How to initialize an array with objects from another array in Swift?

In objective-c for NSArray we have methods:

+ (instancetype)arrayWithObject:(id)anObject;
+ (instancetype)arrayWithArray:(NSArray *)array;

are there similar methods in swift?

Thanks in advance!

Upvotes: 0

Views: 1659

Answers (1)

Antonio
Antonio

Reputation: 72780

No, because they are not needed. Swift arrays are value types, so when you assign an instance of an array to another variable (or pass to a function/method), a copy of it is created and assigned/passed.

So you don't have anything special to do - the problem arises when you need the opposite, as assigning an array by reference is not possible - although it's possible to pass to a function/method by using the inout modifier.

Upvotes: 3

Related Questions