【发布时间】:2023-03-22 07:27:01
【问题描述】:
我是 Swift 的非常新手,所以如果这是一个“愚蠢”的问题,我提前道歉。我正在研究一个用于生成随机项目的游乐场脚本,在这种情况下是武器。当我运行我的代码时,我收到此错误:错误:执行被中断,原因:EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)。我试图做的是持有一个结构的实例(这是句柄)在我的变量武器句柄中的类 normalBladeType 中。我试过研究这个话题,但我还没有找到答案。任何建议都会很棒。据我所知,我可能做错了。
谢谢,
我的代码:
//: Playground - noun: a place where people can play
import Cocoa
let handleWoods = ["White Ash", "Oak", "White Oak", "Elm", "Maple","Walnut", "Cherry", "Rosewood", "Ash", "Hickory", "Birch", "Hemlock", "Cedar", "Pine"]
let handleGrips = ["Leather", "Buckskin", "Sharkskin", "Goat Skin", "Deerskin", "Elk Skin", "Rayskin", "Snakeskin", "Silk Cord", "Cotton Cord"]
let gripQualities = ["Simple", "Interwoven", "Ornate", "Smooth", "Thin", "Thick", "Ruff", "Worn"]
func returnRandomItem( _ list: [Any])-> Any {
return list[Int(UInt32(list.count))]
}
struct handle {
var name: String
var value, grip: Int
var weight: Double
var withGrip: Bool
init(withGrip: Bool) {
self.weight = 0.25
self.withGrip = withGrip
let handleNameWithWood = "\(returnRandomItem(handleWoods)) Handle"
if self.withGrip {
let randGrip = "\(returnRandomItem(gripQualities)) \(returnRandomItem(handleGrips)) Grip)"
self.name = "\(randGrip) (\(handleNameWithWood))"
self.grip = 75
self.value = 2
} else {
self.name = handleNameWithWood
self.grip = 50
self.value = 1
}
}
func description() {
print("Handle Description \(self.name)")
}
}
class weapon {
var TypeOfWeapon: String
var weaponHandle: handle
init(weaponType: String, doesHaveGrip: Bool) {
self.TypeOfWeapon = weaponType
self.weaponHandle = handle(withGrip: doesHaveGrip)
}
}
class normalBladeType: weapon {
init() {
super.init(weaponType: "normalBladeType", doesHaveGrip: false)
}
func description() {
print("TypeOfWeapon: \(self.TypeOfWeapon)")
print("TypeDescription: normal hilt (guard - handle - pommel) + straight blade")
}
}
var foo = normalBladeType()
foo.description()
【问题讨论】:
-
编辑您的问题以包含
returnRandomItem的源代码。
标签: swift class struct initialization instance