Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set (Collection) - Insert multiple elements

Set is an unordered collection of unique elements. Almost similar to array.

I want to add/insert multiple elements in a Set of String. But there is only single method provided that can insert only one element (accepts single Set element as a parameter argument) and I've collection of string (id).

insert(_:)

@discardableResult mutating func insert(_ newMember: Set.Element) -> (inserted: Bool, memberAfterInsert: Set.Element) 

How can I do that?

What I've tried:
I tried to create an extension very similar to insert(_:) method but it can accept multiple Set elements. It would be same as use of iteration over collection but don't need to handle it manually everywhere.

extension Set {      @discardableResult mutating func insert(_ newMembers: [Set.Element]) -> (inserted: Bool, memberAfterInsert: Set.Element) {          newMembers.forEach { (member) in             self.insert(member)         }     } } 

It should work, if I return a tuple as expected but no idea how and where (which line) and what to return a value.

Here is error message.

Missing return in a function expected to return '(inserted: Bool, memberAfterInsert: Set.Element)'

enter image description here

What can be solution to this. Is there any better solution/approach to handle this operation?

like image 469
Krunal Avatar asked Feb 15 '18 18:02

Krunal


People also ask

How do you add multiple elements to a set?

To add multiple elements at once we use the Set update() method. It takes an iterable(list, tuple, dictionary) as an argument. We can add single or multiply iterable in the set using the Update() method.

Which of the method is used to insert multiple elements in list collection?

extend() extend() can add multiple items to a list.

Is set ordered in Swift?

At the time being there is no ordered set in Swift. Despite using NSOrderedSet on all Apple platforms, you can simply combine a Set with an Array to basically get the same effect. The Set is used to avoid duplicate entries, the Array is used to store the order.


Video Answer


2 Answers

It was pointed out in the comments under the question, but I'd like to clearly state that there is a method for that very same purpose:

mutating func formUnion<S>(_ other: S) where Element == S.Element, S : Sequence 

Usage:

var attendees: Set = ["Alicia", "Bethany", "Diana"] let visitors = ["Diana", "Marcia", "Nathaniel"] attendees.formUnion(visitors) print(attendees) // Prints "["Diana", "Nathaniel", "Bethany", "Alicia", "Marcia"]" 

Source: Apple Developer


There is also an immutable variant which returns a new instance containing the union:

func union<S>(_ other: S) -> Set<Set.Element> where Element == S.Element, S : Sequence 

Usage:

let attendees: Set = ["Alicia", "Bethany", "Diana"] let visitors = ["Marcia", "Nathaniel"] let attendeesAndVisitors = attendees.union(visitors) print(attendeesAndVisitors) // Prints "["Diana", "Nathaniel", "Bethany", "Alicia", "Marcia"]" 

Source: Apple Developer

like image 99
Arkadii Avatar answered Sep 18 '22 09:09

Arkadii


a.union(b) - a ∪ b - the result set contains all elements from a and b

union - immutable function
unionInPlace(up to Swift 3) => formUnion - mutable function

[Mutable vs Immutable]

Read more here

like image 22
yoAlex5 Avatar answered Sep 21 '22 09:09

yoAlex5