我在Swift编程并尝试创建一个包含我正在尝试构建的世界中所有位置的地图对象。首先,我实例化了我将在地图中拥有的所有不同位置对象。我需要在某种数据结构中对它们进行索引,但是当我尝试使用实例化位置创建另一个实例变量,矩阵,数组数组时,Swift编译器会给我一个错误。有没有办法绕过这个,或者我不允许使用其他实例变量来创建新的变量?我对OOP有点新意,所以如果这是一个愚蠢的问题,我道歉。总之,非常感谢。
class Map: NSObject {
//Instantiate all the places on your map by instantiating locations
let seaofFrigado = Location(name: "seaofFrigado")
let shippensport = Location(name: "Shippensport")
let drivensFarms = Location(name: "Driven's Farms")
let deadmansRift = Location(name: "Deadman's Rift")
let siccumDesert = Location(name: "Siccum Desert")
let shalkensSands = Location(name: "Shalken's Sands")
let yellowForest = Location(name: "Yellow Forrest")
let arxRei = Location(name: "Arx Rei")
let urbsrei = Location(name: "Urbsrei")
let kaldersCavern = Location(name: "Kalder's Cavern")
let mountRugnor = Location(name: "Mount Rugnor")
let havenValley = Location(name: "Haven Valley")
let tyvanyasOrchard = Location(name: "Tyvanya's Orchard")
let aethalwaldsFarm = Location(name: "Aethelwald's Farm")
let magnoppidumNorth = Location(name: "Magnoppidum North")
let ancientRuins = Location(name: "Ancient Ruins")
let mountVeldunr = Location(name: "Mount Veldunr")
let nomadsPass = Location(name: "Nomad's Pass")
let magnoppidumWest = Location(name: "Magnoppidum West")
let magnoppidumCenter = Location(name: "Magnoppidum Center")
let map: [[Location]] = [[seaofFrigado, shippensport, drivensFarms, deadmansRift, siccumDesert], [shalkensSands, yellowForest, arxRei, urbsrei, kaldersCavern], [mountRugnor, havenValley, tyvanyasOrchard, aethalwaldsFarm, magnoppidumNorth], [ancientRuins, mountVeldunr, nomadsPass, magnoppidumWest, magnoppidumCenter]]
答案 0 :(得分:0)
所有变量都是常量。除非您使用Obj-C代码连接,否则您可能不需要继承NSObject。
尝试这样的格式:
class Map {
var map: [[Location]]
init() {
//Instantiate all the places on your map by instantiating locations
let seaofFrigado = Location(name: "seaofFrigado")
let shippensport = Location(name: "Shippensport")
let drivensFarms = Location(name: "Driven's Farms")
let deadmansRift = Location(name: "Deadman's Rift")
let siccumDesert = Location(name: "Siccum Desert")
let shalkensSands = Location(name: "Shalken's Sands")
let yellowForest = Location(name: "Yellow Forrest")
let arxRei = Location(name: "Arx Rei")
let urbsrei = Location(name: "Urbsrei")
let kaldersCavern = Location(name: "Kalder's Cavern")
let mountRugnor = Location(name: "Mount Rugnor")
let havenValley = Location(name: "Haven Valley")
let tyvanyasOrchard = Location(name: "Tyvanya's Orchard")
let aethalwaldsFarm = Location(name: "Aethelwald's Farm")
let magnoppidumNorth = Location(name: "Magnoppidum North")
let ancientRuins = Location(name: "Ancient Ruins")
let mountVeldunr = Location(name: "Mount Veldunr")
let nomadsPass = Location(name: "Nomad's Pass")
let magnoppidumWest = Location(name: "Magnoppidum West")
let magnoppidumCenter = Location(name: "Magnoppidum Center")
map = [[seaofFrigado, shippensport, drivensFarms, deadmansRift, siccumDesert], [shalkensSands, yellowForest, arxRei, urbsrei, kaldersCavern], [mountRugnor, havenValley, tyvanyasOrchard, aethalwaldsFarm, magnoppidumNorth], [ancientRuins, mountVeldunr, nomadsPass, magnoppidumWest, magnoppidumCenter]]
}
}
var map1 = Map()
var map2 = Map()
这应该可以获得您正在寻找的功能