Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift-如何在类property中存储结构实例_Swift_Class_Struct_Initialization_Instance - Fatal编程技术网

Swift-如何在类property中存储结构实例

Swift-如何在类property中存储结构实例,swift,class,struct,initialization,instance,Swift,Class,Struct,Initialization,Instance,我是Swift的新手,所以如果这是一个“愚蠢”的问题,我很抱歉。我正在编写一个游戏场脚本,用于生成随机项目,在本例中为武器。当我运行我的代码时,我得到了这个错误:错误:执行被中断,原因:EXC_BAD_指令(code=EXC_I386_INVOP,subcode=0x0)。我试图做的是在我的类normalBladeType的变量weaponHandle中保存一个结构实例(即handle)。我试着研究这个话题,但还没有找到答案。任何建议都很好。就我所知,我可能在这件事上做错了 谢谢 我的代码: /

我是Swift的新手,所以如果这是一个“愚蠢”的问题,我很抱歉。我正在编写一个游戏场脚本,用于生成随机项目,在本例中为武器。当我运行我的代码时,我得到了这个错误:错误:执行被中断,原因:EXC_BAD_指令(code=EXC_I386_INVOP,subcode=0x0)。我试图做的是在我的类normalBladeType的变量weaponHandle中保存一个结构实例(即handle)。我试着研究这个话题,但还没有找到答案。任何建议都很好。就我所知,我可能在这件事上做错了

谢谢

我的代码:

//: 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
函数错误。你应该把它改成

func returnRandomItem( _ list: [Any])-> Any {
    let index: UInt32 = arc4random_uniform(UInt32(list.count))
    return list[Int(index)]
}

使用此代码对我来说效果很好。

编辑您的问题,将
returnRandomItem
的源代码包含在内。谢谢,我感觉其他地方可能有问题。我在黑暗中拍摄了一个类中的struct(我试着查看文档,等等,花了一天的时间寻找错误的问题)。