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}}}
I imagine this is because an Optional behaves like a value type. So what you have is a value type of a value type. A value type.
In order to do layout for a value type, you need to know the size of things involved.
So you have (simplified):
sizeof(SplitString) = sizeof(String) + sizeof(SplitString?)
with
sizeof(SplitString?) > sizeof(SplitString)
since you need to store the fact that the value is or is not present (and, no, a pattern of sizeof(SplitString) zeros is not a valid representation for that as it would be for a class type - why? imagine Int? = 0)
So, now you have
sizeof(SplitString?) > sizeof(String) + sizeof(SplitString?)
but all quantities involved are definitely > 0. Rather the absurd, right?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With