2
返回我有一個應用程序中,我想創建一個基於是否已經有保存到用戶的默認等同對象的對象。如果檢測到對象,我想在類init中檢測到它並提前返回。這就是我要做的:如何從早期一個init在迅速3
init() {
/* There are two possibilities when creating a hero:
1. The hero is brand new and needs to be built from scratch
2. The hero is loaded from defaults */
// Check to see if there is existing game data:
if defaultExistsForGameData() {
// This means there is a hero to load and no need to create a new one
self = extractHeroFromDefaults() // This just loads from UserDefaults
print("Loading hero from defaults with name of: \(hero.heroName).")
return self
}
// These actions are for creating a brand new hero
let size = CGSize(width: 32, height: 32)
let heroTexture = SKTexture(imageNamed: "hero2.ico")
super.init(texture: heroTexture, color: .clear, size: size)
self.isUserInteractionEnabled = true
self.name = "hero"
self.zPosition = 50
}
有在控制檯一對夫婦的錯誤,自己是不可改變的,等我想知道這是否是一個有效的模式是什麼,或者我應該完全考慮不同的方法。
感謝這正是我需要的分析。我仍然遇到的一個麻煩是,即使我將自定義對象保存爲默認值,並且可以_seemingly_加載它,但是在創建對象時,屬性都是默認值。我是否需要採取其他一些措施,如實施NSCoding,或者什麼? – zeeple
這取決於你如何將它寫入用戶默認值。儘管一般情況下如果你的方法需要'NSCoding',那麼如果不這樣做,它就不會工作。我會確保你實際上正在寫你認爲你正在寫的東西,並且你實際上是從你正在閱讀的內容中創造出一種價值。很多'print'語句或斷點在這裏是你的朋友。 –