2015-04-01 140 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") 
}}