TL DR:
What you are trying to achieve is easily accomplished with split:
for s in split("first/second/third", { c in c == "/" } ) { println("\(s)") }
Discussion
It looks like you are trying to write a linked list of value types. The problem is that Swift combines the concepts of copy semantics with value / reference access. (unlike C ++, which allows you to create the same object on the stack or heap). The solution seems to be wrapping it in a aka class reference container.
class SplitString { //splits a string into parts before and after the first "/" var preSlash: String = String() var postSlash: Wrapped? = nil init(_ str: String) { var arr = Array(str) var x = 0 for ; x < arr.count && arr[x] != "/"; x++ { preSlash.append(arr[x]) } if x + 1 < arr.count { //if there is a slash var postSlashStr = String() for x++; x < arr.count; x++ { postSlashStr.append(arr[x]) } postSlash = Wrapped(postSlashStr) } } } class Wrapped { var split:SplitString init(var _ str:String) { split = SplitString(str) } }
Please note that this code compiles as a proof of concept, but I have not delved into your algorithm or tested it.
Edit:
In response to your changes above, this code will execute your code above and give the splits you need:
for (var s:SplitString? = split; s != nil; s = s?.postSlash?.split) { println("\(s!.preSlash)") }
Obviously, turtles completely omitted does not make sense in the discussion above, so you need to break the loop, as was the case with the class containing your structure.
Please note that I tried to answer the question that you posted, not what you have. The solution to the problem that you have is to use SequenceOf and GeneratorOf to create a generator that completes the sequence that iterates through the slashes and returns the substrings between them. This is really done for you through the split function:
for s in split("first/second/third", { c in c == "/" } ) { println("\(s)") }
source share