SLN
SLN

Reputation: 5092

No last property for "set" any more?

The book I've read claims that "You can use the first and last properties, which return one of the elements in the set".

However, I've tried it and seems it is not working. I would very appreciate if someone can explain it for me.

enter image description here

Upvotes: 3

Views: 3284

Answers (2)

Vladislav Kovalyov
Vladislav Kovalyov

Reputation: 803

You can revert it back to Array which has last property. Do it this way:

let last = Array(Set([1, 2, 3, 4, 5])).last

Upvotes: 0

Julien Quere
Julien Quere

Reputation: 2459

You cannot ask for the last item of a Set. Indeed, a Set is not ordered, so there is not last item (nor last property).

Don't hesitate to take a look at the Swift Doc on Set.

If you want to find the last element, you need an ordered collection like an Array:

var myArray = [1, 2, 3, 4, 5]
print(myArray.last) // Display 5

Upvotes: 5

Related Questions