我无法在 Swift 中的 class 中创建空数组 属性
I can't create a empty array property in a class in Swift
我想在 Xcode Swift 中创建一个 class 请参阅此代码:
class Cards {
var roundNumber = 10
var monsters:[
(Id: Int, Name:String, Description: String, Attack_min: Int, Attack_max: Int, Health: Int, VP: Int, Gold:Int)] = [
(Id: 1, Name: "Ice dragon", Description: "Attack on Wizards +1", Attack_min: 2, Attack_max: 5, Health: 4, VP: 2, Gold: 4),
(Id: 2, Name: "Fire dragon", Description: "Attack on Wariors +2", Attack_min: 2, Attack_max: 5, Health: 4, VP: 2, Gold: 4),
(Id: 3, Name: "Acid witch", Description: "Attack of all heroes -1 \n(Only -1 on heroes)", Attack_min: 3, Attack_max: 6, Health: 5, VP: 4, Gold: 5),
(Id: 4, Name: "Killer rats", Description: "Health -1 when attacked by supporting cards ", Attack_min: 2, Attack_max: 6, Health: 3, VP: 1, Gold: 3)
]
var monsterDeck = [Any]()
func randomizer(lowestNumber: Int, highestNumber: Int) -> Int {
return Int.random(in: lowestNumber ... highestNumber)
}
func round() {
if self.roundNumber == 10 {
self.monsters.shuffle()
self.monsterDeck = self.monsters
} else {
self.monsterDeck.removeFirst()
}
self.roundNumber -= 1
}
}
var cards = Cards()
cards.round()
print("\(cards.monsterDeck[0].Name)\n")
print(cards.monsterDeck)
print(cards.roundNumber)
var monsters 是我要自己更新的数组
我想洗牌并将其存储在 var monsterDeck
我想要的是每次我 运行 函数 round() 我想要一张新的洗牌的怪物卡并且应该删除第一个 monsterdeck 数组。
但在底部您会看到 print("\(cards.monsterDeck[0].Name)\n")
,为此我得到一个错误:
error: MyPlayground.playground:34:31: error: value of type 'Any' has no member 'Name'
print("\(cards.monsterDeck[0].Name)\n")
那我该如何解决呢?基本上我希望 var monsterDeck 是一个数组,它是数组 monsters
的随机版本
我检查了 ,但这些答案假设数组只是字符串或整数。
我的示例有字符串和整数字段
为您的 Monster
数据使用结构,而不是元组,然后创建这些结构的数组:
var monsterDeck = [Monster]()
通过使用 Any
作为类型,您将丢失有关牌组内容的所有详细信息。
我想在 Xcode Swift 中创建一个 class 请参阅此代码:
class Cards {
var roundNumber = 10
var monsters:[
(Id: Int, Name:String, Description: String, Attack_min: Int, Attack_max: Int, Health: Int, VP: Int, Gold:Int)] = [
(Id: 1, Name: "Ice dragon", Description: "Attack on Wizards +1", Attack_min: 2, Attack_max: 5, Health: 4, VP: 2, Gold: 4),
(Id: 2, Name: "Fire dragon", Description: "Attack on Wariors +2", Attack_min: 2, Attack_max: 5, Health: 4, VP: 2, Gold: 4),
(Id: 3, Name: "Acid witch", Description: "Attack of all heroes -1 \n(Only -1 on heroes)", Attack_min: 3, Attack_max: 6, Health: 5, VP: 4, Gold: 5),
(Id: 4, Name: "Killer rats", Description: "Health -1 when attacked by supporting cards ", Attack_min: 2, Attack_max: 6, Health: 3, VP: 1, Gold: 3)
]
var monsterDeck = [Any]()
func randomizer(lowestNumber: Int, highestNumber: Int) -> Int {
return Int.random(in: lowestNumber ... highestNumber)
}
func round() {
if self.roundNumber == 10 {
self.monsters.shuffle()
self.monsterDeck = self.monsters
} else {
self.monsterDeck.removeFirst()
}
self.roundNumber -= 1
}
}
var cards = Cards()
cards.round()
print("\(cards.monsterDeck[0].Name)\n")
print(cards.monsterDeck)
print(cards.roundNumber)
var monsters 是我要自己更新的数组 我想洗牌并将其存储在 var monsterDeck
我想要的是每次我 运行 函数 round() 我想要一张新的洗牌的怪物卡并且应该删除第一个 monsterdeck 数组。
但在底部您会看到 print("\(cards.monsterDeck[0].Name)\n")
,为此我得到一个错误:
error: MyPlayground.playground:34:31: error: value of type 'Any' has no member 'Name'
print("\(cards.monsterDeck[0].Name)\n")
那我该如何解决呢?基本上我希望 var monsterDeck 是一个数组,它是数组 monsters
的随机版本我检查了
我的示例有字符串和整数字段
为您的 Monster
数据使用结构,而不是元组,然后创建这些结构的数组:
var monsterDeck = [Monster]()
通过使用 Any
作为类型,您将丢失有关牌组内容的所有详细信息。