I have a structure in which I want to have a global variable of type Struct?. This example is essentially a briefer version of the structure I am actually creating.
struct SplitString { //splits a string into parts before and after the first "/"
var preSlash: String = String()
var postSlash: SplitString? = 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 = SplitString(postSlashStr)
}
}
}
However, it throws the error:
Recursive value type 'SplitString' is not allowed
Is there any way to get around this? Any help would be great. Thanks :)
Edit: In case it is relevant, I am programming on iOS, not OSX.
Edit: If I have:
var split = SplitString("first/second/third")
I would expect split to be:
{preSlash = "first", postSlash = {preSlash = "second", postSlash = {preSlash = "third",
postSlash = nil}}}
TL;DR:
What you are trying to achieve is easily accomplished using split:
Discussion:
It seems like you are trying to write a linked list of value types. The problem is that Swift couples the concepts of copy semantics with value / reference access. (unlike say C++ which allows you to create the same object on the stack or heap). The solution would seem to be wrapping it in a reference container aka class.
Note that this code compiles as a proof of concept but I haven't delved into your algorithm or tested it.
Edit:
In response to your edits above, this code will exercise your code above and yield the splits you want:
Obviously having turtles all the way down doesn't make sense, per the discussion above, so you need to break the cycle, as was the case with the class containing your struct.
Note that I have been trying to answer the question that you posted, not the problem that you have. The solution to the problem that you have, is to use SequenceOf and GeneratorOf to create a sequence wrapped generator that iterates through the slashes and returns the substrings between. This is actually done for you via the split function: