我想知道如何在 Swift 中初始化自定义类型的双向数组
I would please like to know how to initialize a bidimenstional array of a custom type in Swift
具体例子如下:
enum Segments {
case s1, s2, s3, noParent, empty
}
struct StructA {
var id: Segments = .empty
var parent: Segments = .empty
var value = 0.0
}
class ClassA {
let StructAMap: Array<Array<StructA>> = []
init {
for help in 1...2 {
StructAMap[help][1].id = Segments.s1
StructAMap[help][1].parent = Segments.noParent
StructAMap[help][1].value = 0.0
StructAMap[help][2].id = Segments.s2
StructAMap[help][2].parent = Segments.s1
// etc...
}
StructAMap[1][3].id = Segments.s1
StructAMap[2][3].value = 3.0
// etc...
}
}
如果我尝试这样做,我会收到此错误:无法分配给 'Segments'
类型的不可变值
如果我将 StructAMap 更改为 var 它实际上不起作用,但我收到的错误消息较少
这是结构图,不会更改;这就是为什么我想要它作为一个常数。
非常感谢。
更新:评论 #1
这似乎解决了一些错误错误并且非常聪明。所以现在我正在操场上尝试。我想测试地图是否正常工作,但遇到执行失败:
enum Segments {
case s1, s2, s3, noParent, empty
}
struct StructA {
var id: Segments = .empty
var parent: Segments = .empty
var value = 0.0
}
class ClassA {
let StructAMap: Array<Array<StructA>>
init() {
var StructAMap: Array<Array<StructA>> = []
for help in 1...2 {
StructAMap[help][1].id = Segments.s1
StructAMap[help][1].parent = Segments.noParent
StructAMap[help][1].value = 0.0
StructAMap[help][2].id = Segments.s2
StructAMap[help][2].parent = Segments.s1
}
StructAMap[1][3].id = Segments.s1
StructAMap[2][3].value = 3.0
self.StructAMap = StructAMap
}
}
var paciente1 = ClassA()
println("\(paciente1.StructAMap[1][1])")
控制台输出:
游乐场执行失败:执行被中断,原因:EXC_BAD_INSTRUCTION(代码=EXC_I386_INVOP,子代码=0x0)。
* 线程 #1:tid = 0xcea9d,0x000000010c09a14e PlaygroundLoggerSwift.Array.subscript.nativePinningMutableAddressor (Swift.Int) -> A + 238, queue = 'com.apple.main-thread', stop reason = EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)
* frame #0: 0x000000010c09a14e PlaygroundLogger
Swift.Array.subscript.nativePinningMutableAddressor (Swift.Int) -> A + 238
帧 #1: 0x0000000116937e3f $lldb_expr74__lldb_expr_74.ClassA.init (self=0x00007f905a5000e0)() -> __lldb_expr_74.ClassA + 1695 at MyPlayground.playground:122
frame #2: 0x0000000116938444 $__lldb_expr74
__lldb_expr_74.ClassA.__allocating_init ($metatype=0x000000011693ac40)() -> __lldb_expr_74.ClassA + 116 at :0
帧#3:0x00000001169370df $__lldb_expr74main + 223 at MyPlayground.playground:134
frame #4: 0x000000010bb32710 MyPlayground
frame #5: 0x000000010bb365f1 MyPlayground
从@callee_owned () -> (@unowned ()) 到@callee_owned (@in ()) -> (@out ()) + 17
frame #6: 0x000000010bb351f1 MyPlaygroundpartial apply forwarder for reabstraction thunk helper from @callee_owned () -> (@unowned ()) to @callee_owned (@in ()) -> (@out ()) + 81
frame #7: 0x000000010bb36620 MyPlayground
从@callee_owned (@in ()) -> (@out ()) 到@callee_owned () -> (@unowned ( )) + 32
第 8 帧:0x000000010bb36657 MyPlaygroundreabstraction thunk helper from @callee_owned () -> (@unowned ()) to @callee_unowned @objc_block () -> (@unowned ()) + 39
frame #9: 0x000000010c17f41c CoreFoundation
__CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK + 12
第 10 帧:0x000000010c175165 CoreFoundation__CFRunLoopDoBlocks + 341
frame #11: 0x000000010c174923 CoreFoundation
__CFRunLoopRun + 851
第 12 帧:0x000000010c174366 CoreFoundationCFRunLoopRunSpecific + 470
frame #13: 0x000000010c222661 CoreFoundation
CFRunLoopRun + 97
第 14 帧:0x000000010bb32dc2 MyPlaygroundmain + 1714
frame #15: 0x000000010ee0d145 libdyld.dylib
start + 1
第 16 帧:0x000000010ee0d145 libdyld.dylib`start + 1
更新 2:
我想我找到了进一步调整的方法。
class ClassA {
let xSize = 6 // whatever
let ySize = 6 // whatever
static let StructAMap: Array<Array<StructA>> = {
var tempStructAMap = Array(count: ySize, repeatedValue:
Array(count: xSize, repeatedValue: StructA()))
for help in 1...2 {
tempStructAMap[help][1].id = Segments.s1
tempStructAMap[help][1].parent = Segments.noParent
tempStructAMap[help][1].value = 0.0
tempStructAMap[help][2].id = Segments.s2
tempStructAMap[help][2].parent = Segments.s1
// etc...
}
tempStructAMap[1][3].id = Segments.s1
tempStructAMap[2][3].value = 3.0
// etc...
return tempStructAMap
}()
}
在 init() 中,使用局部变量构建数组,然后将其分配给实例常量:
class ClassA {
let xSize = 6 // whatever
let ySize = 6 // whatever
let StructAMap: Array<Array<StructA>>
init() {
var StructAMap = Array(count: ySize, repeatedValue:
Array(count: xSize, repeatedValue: StructA()))
for help in 1...2 {
StructAMap[help][1].id = Segments.s1
StructAMap[help][1].parent = Segments.noParent
StructAMap[help][1].value = 0.0
StructAMap[help][2].id = Segments.s2
StructAMap[help][2].parent = Segments.s1
// etc...
}
StructAMap[1][3].id = Segments.s1
StructAMap[2][3].value = 3.0
// etc...
self.StructAMap = StructAMap
}
}
具体例子如下:
enum Segments {
case s1, s2, s3, noParent, empty
}
struct StructA {
var id: Segments = .empty
var parent: Segments = .empty
var value = 0.0
}
class ClassA {
let StructAMap: Array<Array<StructA>> = []
init {
for help in 1...2 {
StructAMap[help][1].id = Segments.s1
StructAMap[help][1].parent = Segments.noParent
StructAMap[help][1].value = 0.0
StructAMap[help][2].id = Segments.s2
StructAMap[help][2].parent = Segments.s1
// etc...
}
StructAMap[1][3].id = Segments.s1
StructAMap[2][3].value = 3.0
// etc...
}
}
如果我尝试这样做,我会收到此错误:无法分配给 'Segments'
类型的不可变值如果我将 StructAMap 更改为 var 它实际上不起作用,但我收到的错误消息较少 这是结构图,不会更改;这就是为什么我想要它作为一个常数。
非常感谢。
更新:评论 #1
这似乎解决了一些错误错误并且非常聪明。所以现在我正在操场上尝试。我想测试地图是否正常工作,但遇到执行失败:
enum Segments {
case s1, s2, s3, noParent, empty
}
struct StructA {
var id: Segments = .empty
var parent: Segments = .empty
var value = 0.0
}
class ClassA {
let StructAMap: Array<Array<StructA>>
init() {
var StructAMap: Array<Array<StructA>> = []
for help in 1...2 {
StructAMap[help][1].id = Segments.s1
StructAMap[help][1].parent = Segments.noParent
StructAMap[help][1].value = 0.0
StructAMap[help][2].id = Segments.s2
StructAMap[help][2].parent = Segments.s1
}
StructAMap[1][3].id = Segments.s1
StructAMap[2][3].value = 3.0
self.StructAMap = StructAMap
}
}
var paciente1 = ClassA()
println("\(paciente1.StructAMap[1][1])")
控制台输出:
游乐场执行失败:执行被中断,原因:EXC_BAD_INSTRUCTION(代码=EXC_I386_INVOP,子代码=0x0)。
* 线程 #1:tid = 0xcea9d,0x000000010c09a14e PlaygroundLoggerSwift.Array.subscript.nativePinningMutableAddressor (Swift.Int) -> A + 238, queue = 'com.apple.main-thread', stop reason = EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)
* frame #0: 0x000000010c09a14e PlaygroundLogger
Swift.Array.subscript.nativePinningMutableAddressor (Swift.Int) -> A + 238
帧 #1: 0x0000000116937e3f $lldb_expr74__lldb_expr_74.ClassA.init (self=0x00007f905a5000e0)() -> __lldb_expr_74.ClassA + 1695 at MyPlayground.playground:122
frame #2: 0x0000000116938444 $__lldb_expr74
__lldb_expr_74.ClassA.__allocating_init ($metatype=0x000000011693ac40)() -> __lldb_expr_74.ClassA + 116 at :0
帧#3:0x00000001169370df $__lldb_expr74main + 223 at MyPlayground.playground:134
frame #4: 0x000000010bb32710 MyPlayground
frame #5: 0x000000010bb365f1 MyPlayground
从@callee_owned () -> (@unowned ()) 到@callee_owned (@in ()) -> (@out ()) + 17
frame #6: 0x000000010bb351f1 MyPlaygroundpartial apply forwarder for reabstraction thunk helper from @callee_owned () -> (@unowned ()) to @callee_owned (@in ()) -> (@out ()) + 81
frame #7: 0x000000010bb36620 MyPlayground
从@callee_owned (@in ()) -> (@out ()) 到@callee_owned () -> (@unowned ( )) + 32
第 8 帧:0x000000010bb36657 MyPlaygroundreabstraction thunk helper from @callee_owned () -> (@unowned ()) to @callee_unowned @objc_block () -> (@unowned ()) + 39
frame #9: 0x000000010c17f41c CoreFoundation
__CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK + 12
第 10 帧:0x000000010c175165 CoreFoundation__CFRunLoopDoBlocks + 341
frame #11: 0x000000010c174923 CoreFoundation
__CFRunLoopRun + 851
第 12 帧:0x000000010c174366 CoreFoundationCFRunLoopRunSpecific + 470
frame #13: 0x000000010c222661 CoreFoundation
CFRunLoopRun + 97
第 14 帧:0x000000010bb32dc2 MyPlaygroundmain + 1714
frame #15: 0x000000010ee0d145 libdyld.dylib
start + 1
第 16 帧:0x000000010ee0d145 libdyld.dylib`start + 1
更新 2:
我想我找到了进一步调整的方法。
class ClassA {
let xSize = 6 // whatever
let ySize = 6 // whatever
static let StructAMap: Array<Array<StructA>> = {
var tempStructAMap = Array(count: ySize, repeatedValue:
Array(count: xSize, repeatedValue: StructA()))
for help in 1...2 {
tempStructAMap[help][1].id = Segments.s1
tempStructAMap[help][1].parent = Segments.noParent
tempStructAMap[help][1].value = 0.0
tempStructAMap[help][2].id = Segments.s2
tempStructAMap[help][2].parent = Segments.s1
// etc...
}
tempStructAMap[1][3].id = Segments.s1
tempStructAMap[2][3].value = 3.0
// etc...
return tempStructAMap
}()
}
在 init() 中,使用局部变量构建数组,然后将其分配给实例常量:
class ClassA {
let xSize = 6 // whatever
let ySize = 6 // whatever
let StructAMap: Array<Array<StructA>>
init() {
var StructAMap = Array(count: ySize, repeatedValue:
Array(count: xSize, repeatedValue: StructA()))
for help in 1...2 {
StructAMap[help][1].id = Segments.s1
StructAMap[help][1].parent = Segments.noParent
StructAMap[help][1].value = 0.0
StructAMap[help][2].id = Segments.s2
StructAMap[help][2].parent = Segments.s1
// etc...
}
StructAMap[1][3].id = Segments.s1
StructAMap[2][3].value = 3.0
// etc...
self.StructAMap = StructAMap
}
}