devios devios - 1 year ago 185
Swift Question

Swift get tailing subarray

Swift's implementation of arrays is throwing me for a loop again! (Haw, haw.) All I want is to take an array of arbitrary length and get a new array (or an array slice) from it that has the first element removed.

Why is this so hard?

I just want to be able to do something like this:

let suffix = someArray.suffixFrom(1)

But I can't figure out how to do this. It seems the closest method I've found requires knowing the length of the array, which I don't know because it's computed inline and I really really hate having to split such a simple concept up into a bunch of variables and lines.


I have a string split by colons (:) and I just want to create a
containing all the :-delimited components excluding the first one.

So that a string "one:two:three" would return a set containing ["two", "three"]. Since I can create a Set from an array, all I really need is to get the suffix of the array, but I don't know how many components there are because it's inline:

return Set(attributeName?.componentsSeparatedByString(":").suffixFrom(1))

Why does Swift make this so hard?

Edit: Before a bunch of you suggest it, I'm well aware I could extend the array class to do this myself, but I'm writing a framework and I don't want to do that, and I also don't want to have to write a bloody utility function to do something so darned simple.

Answer Source

The CollectionType.dropFirst(_:) does exactly what you need, with the exact syntax you're looking for.

let suffix = someArray.dropFirst(1)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download