我有一个结构,我想在其中有一个 Struct? 类型的全局变量。这个例子本质上是我实际创建的结构的简短版本。
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)
}
}
}
但是,它会抛出错误:
Recursive value type 'SplitString' is not allowed
有什么办法可以解决这个问题吗?任何帮助都会很棒。谢谢 :)
编辑:
如果相关的话,我是在 iOS 上编程,而不是 OSX。
编辑:
如果我有:
var split = SplitString("first/second/third")
我希望拆分为:
{preSlash = "first", postSlash = {preSlash = "second", postSlash = {preSlash = "third",
postSlash = nil}}}
我想这是因为可选的行为就像值类型。
所以你拥有的是值类型的值类型。一种值类型。
为了对值类型进行布局,您需要知道所涉及的内容的大小。
所以你有(简化的):
sizeof(SplitString) = sizeof(String) + sizeof(SplitString?)
with
sizeof(SplitString?) > sizeof(SplitString)
因为您需要存储该值存在或不存在的事实(并且,不, sizeof(SplitString) 零的模式不是该值的有效表示,因为它对于类类型来说是有效的表示形式 - 为什么?想象一下 Int?= 0)
所以,现在你有
sizeof(SplitString?) > sizeof(String) + sizeof(SplitString?)
但所有涉及的数量肯定> 0。这很荒谬,对吧?
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)