2015-04-01 193 views
-1

我正在构建一个快速的游戏并需要设置一个类。我的代码适用于我班的所有元素,但不适用于此。使用NSuserdefaults的Swift中的信号SIGABRT

func saveInformationMember(){ 

var MembersDefaultName = NSUserDefaults.standardUserDefaults() 
MembersDefaultName.setValue(globalCurrentMembers, forKey: "globalCurrentMembersData") 
MembersDefaultName.synchronize() 

}

GlobalCurrentMembersMember至极的阵列看起来像这样:

class Member { 
var image = String() 
var name = String() 
var progression = Int() 
var round = Int() 
var level = Int() 
var imageProgression = [UIButton]() 


func Init(){ 
    image = "default.png" 
    name = "default" 
    progression = 0 
    round = 0 
    level = 0 
} 

THX提前的帮助。

+0

这些建议都不能工作。 – 2015-04-01 19:24:06

+1

我明白了。 Thx的意见;) – 2015-04-01 19:45:00

回答

0

为了以这种方式保存班级,Member需要符合NSCoding协议。

+0

谢谢你,我会阅读关于'NSCoding' – 2015-04-01 19:25:48

0

Thx to Aaron Brager for is response。这是响应:

func saveInformationMember(){ 
let data = NSKeyedArchiver.archivedDataWithRootObject(globalCurrentMembers) 
NSUserDefaults.standardUserDefaults().setObject(data, forKey: "member")} 

func loadInformationMember(){ 

if let data = NSUserDefaults.standardUserDefaults().objectForKey("member") as? NSData { 
globalCurrentMembers = NSKeyedUnarchiver.unarchiveObjectWithData(data) as [Member] 
} 

而我的等级:

class Member : NSObject, NSCoding { 
var image = String() 
var name = String() 
var progression = Int() 
var round = Int() 
var level = Int() 
var imageProgression = [UIButton]() 


func initiation(){ 
    image = "default.png" 
    name = "default" 
    progression = 0 
    round = 0 
    level = 0 
} 

required convenience init(coder decoder: NSCoder) { 
    self.init() 
    self.image = decoder.decodeObjectForKey("image") as String! 
    self.name = decoder.decodeObjectForKey("name") as String! 
    self.progression = decoder.decodeIntegerForKey("progression") as Int! 
    self.round = decoder.decodeIntegerForKey("round") as Int! 
    self.level = decoder.decodeIntegerForKey("level") as Int! 
} 

func encodeWithCoder(coder: NSCoder) { 
    coder.encodeObject(self.image, forKey: "image") 
    coder.encodeObject(self.name, forKey: "name") 
    coder.encodeInt(Int32(self.progression), forKey: "progression") 
    coder.encodeInt(Int32(self.round), forKey: "round") 
    coder.encodeInt(Int32(self.level), forKey: "level") 
}}