我创建了一个自定义对象,它由字符串int,UIImage组成。我试图添加一个NSMutable数组,当试图运行我的项目时,我收到了一个错误。 “线程1:EXC_BAD_INSTRUCTION(代码= EXC_1386_INVOP,子代码= 0x0)”。 如何防止此错误并在此类中使用此nsmutablearray?
这是我的班级:
class develop : NSObject, NSCoding {
var power: Int!
var name: String!
var image: UIImage!
var flag: UIImage!
var militaryName: String!
var relations: NSMutableArray!
override init() {
super.init()
}
func encodeWithCoder(aCoder: NSCoder) {
aCoder.encodeInteger(power, forKey:"power")
aCoder.encodeObject(name, forKey:"name")
aCoder.encodeObject(image, forKey:"image")
aCoder.encodeObject(flag, forKey: "flag")
aCoder.encodeObject(militaryName, forKey: "militaryName")
aCoder.encodeObject(relations, forKey: "relations")
}
required init(coder aDecoder: NSCoder) {
super.init()
power = aDecoder.decodeIntegerForKey("power")
name = aDecoder.decodeObjectForKey("name") as! String
image = aDecoder.decodeObjectForKey("image")as! UIImage
flag = aDecoder.decodeObjectForKey("flag") as! UIImage
militaryName = aDecoder.decodeObjectForKey("militaryName") as! String
relations = aDecoder.decodeObjectForKey("relations") as! NSMutableArray
}
}
这里是对象的设置:
func setUpDefaults(){
//Afghanistan
Afghanistan.power = 0
Afghanistan.name = "Afghanistan"
Afghanistan.image = UIImage(named: "afghan_flag.jpg")!
Afghanistan.flag = UIImage(named: "afghan_flag.jpg")!
Afghanistan.militaryName = "Afghan Military"
Afghanistan.relations = [New_Zealand]
}
这是装载:
class Test:UIViewController {
@IBOutlet var Label: UILabel!
@IBOutlet var imageView: UIImageView!
override func viewDidLoad() {
//Afganistan
setUpDefaults()
let developData = NSKeyedArchiver.archivedDataWithRootObject(Afghanistan)
NSUserDefaults().setObject(developData, forKey: "Afg")
if let loadedData = NSUserDefaults().dataForKey("Afg") {
if let loadedDevelop = NSKeyedUnarchiver.unarchiveObjectWithData(loadedData) as? develop{
Label.text = Afghanistan.militaryName
imageView.image = loadedDevelop.flag
}
}
}
}