2017-07-04 79 views
0

我是非常 Swift新手,所以如果這是一個「啞巴」問題,我很抱歉。我只是在製作隨機物品的遊樂場腳本,在這裏是武器。當我運行我的代碼時,出現以下錯誤:錯誤:執行中斷,原因:EXC_BAD_INSTRUCTION(code = EXC_I386_INVOP,subcode = 0x0)。我想要做的是在我的類normalBladeType中的變量weaponHandle中保存一個結構(這是句柄)的一個實例。我試過研究這個話題,但我還沒有找到答案。任何建議都會很棒。就我所知,我可能會對這一切都說錯了。Swift - 如何在類中存儲結構實例

感謝,

我的代碼:

//: 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() 
+0

編輯您的問題以包含'returnRandomItem'的源代碼。 –

回答

0

returnRandomItem功能是錯誤的。您應該將其更改爲

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

它使用此代碼正常工作。

+0

謝謝,我有一種感覺,其他地方可能存在問題。我在黑暗中使用了一個類的東西(我試着查看文檔等,花了一天的時間尋找錯誤的結果)。 – JHuggett

+0

不客氣。感謝接受 – adev

相關問題