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).
@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)'
What can be solution to this. Is there any better solution/approach to handle this operation?
insert(_:)
for single Set element. I want to create a same function but it should accept multple Set elements. – Bicycleinserted: true
? When one value is inserted? There is no "same function" that behaves differently. It's up to you to decide what the function returns. When all the values are inserted? What does your use case need thatformUnion
doesn't provide? In any case, as Milan notes, if you want to return that, you need to actually return that. – Metalware