2016-03-18 13 views
1
import UIKit 

struct PropertyKeys { 
    static let keyHightScore = "hightScore" 
    static let keyLastScore = "lastScore" 
    static let keyCurrentScore = "currentScore" 
} 

class GameSetting: NSObject, NSCoding { 

    var hightScore: Int 
    var currentScore: Int 
    var lastScore: Int 

    var life = 3 

    override init() { 
     hightScore = 0 
     currentScore = 0 
     lastScore = 0 

     super.init() 

     loadGameSetting() 
    } 

    func encodeWithCoder(aCoder: NSCoder) { 
     aCoder.encodeInteger(hightScore, forKey: PropertyKeys.keyHightScore) 
     aCoder.encodeInteger(currentScore, forKey: PropertyKeys.keyCurrentScore) 
     aCoder.encodeInteger(lastScore, forKey: PropertyKeys.keyLastScore) 
    } 

    required convenience init?(coder aDecoder: NSCoder) { 
     var hightScore = aDecoder.decodeIntegerForKey(PropertyKeys.keyHightScore) 
     var currentScore = aDecoder.decodeIntegerForKey(PropertyKeys.keyCurrentScore) 
     var lastScore = aDecoder.decodeIntegerForKey(PropertyKeys.keyLastScore) 

     self.init(hightScore: Int, currentScore: Int, lastScore: Int) { 
      self.hightScore = hightScore 
      self.currentScore = currentScore 
      self.lastScore = lastScore 
     } 
    } 

    func recordScore(score: Int) { 
     if score > hightScore { hightScore = score } 

     lastScore = score 

     saveGameSetting() 
    } 

    func saveGameSetting() { 
     NSUserDefaults.standardUserDefaults().setInteger(hightScore, forKey: PropertyKeys.keyHightScore) 
     NSUserDefaults.standardUserDefaults().setInteger(lastScore, forKey: PropertyKeys.keyLastScore) 
    } 

    func loadGameSetting() { 
     hightScore = NSUserDefaults.standardUserDefaults().integerForKey(PropertyKeys.keyHightScore) 
     lastScore = NSUserDefaults.standardUserDefaults().integerForKey(PropertyKeys.keyLastScore) 
    } 

    override var description: String { 
     return "HighScore: \(self.hightScore), lastScore: \(self.lastScore), currenScore: \(self.currentScore)" 
    } 

    func reset() { 
     currentScore = 0 
    } 

    func resetHightScore() { 
     hightScore = 0 
     lastScore = 0 

     saveGameSetting() 
    } 
} 

Sorun nedir? Bu hatayı anlamadım.'GameSetting.init' bir argüman listesi ile çağrlanılamıyor

Bu konuda bir şey bulamıyorum.

Ben verileri kaydetmek ve yük puanı bitmiş istiyorum ve ben bu örnekte şu duyuyorum: Alanların argümanlarla bir init yöntemi tanımlamadınız https://developer.apple.com/library/ios/referencelibrary/GettingStarted/DevelopiOSAppsSwift/Lesson10.html

+0

'GameSetting.init' türünde bir argüman listesiyle çağrlanılamıyor '(hightScore: Int.Type, currentScore: Int.Type, lastScore: Int.Type,() ->())' –

cevap

3

. Muhtemelen böyle bir şey istiyorum:

required convenience init?(coder aDecoder: NSCoder) { 
    let hightScore = aDecoder.decodeIntegerForKey(PropertyKeys.keyHightScore) 
    let currentScore = aDecoder.decodeIntegerForKey(PropertyKeys.keyCurrentScore) 
    let lastScore = aDecoder.decodeIntegerForKey(PropertyKeys.keyLastScore) 

    self.init() 

    self.hightScore = hightScore 
    self.currentScore = currentScore 
    self.lastScore = lastScore 
} 

veya belirtilen bağımsız değişken içeren bir kolaylık init oluşturmak: Bir yanlış self ekleyerek yol açan '}' bir yanlış var gibi

convenience init(highScore: Int, currentScore: Int, lastScore: Int) { 
    self.init() 
    self.hightScore = highScore 
    self.currentScore = currentScore 
    self.lastScore = lastScore 
} 
1

görünüyor sahip

:

required convenience init?(coder aDecoder: NSCoder) { 
    var hightScore = aDecoder.decodeIntegerForKey(PropertyKeys.keyHightScore) 
    var currentScore = aDecoder.decodeIntegerForKey(PropertyKeys.keyCurrentScore) 
    var lastScore = aDecoder.decodeIntegerForKey(PropertyKeys.keyLastScore) 

    self.init(hightScore: Int, currentScore: Int, lastScore: Int) { 
     self.hightScore = hightScore 
     self.currentScore = currentScore 
     self.lastScore = lastScore 
    } 
} 

istediğin düşünüyorum:

required convenience init?(coder aDecoder: NSCoder) { 
    var hightScore = aDecoder.decodeIntegerForKey(PropertyKeys.keyHightScore) 
    var currentScore = aDecoder.decodeIntegerForKey(PropertyKeys.keyCurrentScore) 
    var lastScore = aDecoder.decodeIntegerForKey(PropertyKeys.keyLastScore) 

    self.init(hightScore:hightScore, currentScore:currentScore, lastScore:lastScore) 
} 

init(hightScore: Int, currentScore: Int, lastScore: Int) { 
    self.hightScore = hightScore 
    self.currentScore = currentScore 
    self.lastScore = lastScore 
} 

ve init başarısız, bu nedenle failable olmak için hiçbir neden yok, ve vars değişti alamadım, bu yüzden onları let yapmak için zaten var gibi görünmüyor:

required convenience init(coder aDecoder: NSCoder) { 
    let hightScore = aDecoder.decodeIntegerForKey(PropertyKeys.keyHightScore) 
    let currentScore = aDecoder.decodeIntegerForKey(PropertyKeys.keyCurrentScore) 
    let lastScore = aDecoder.decodeIntegerForKey(PropertyKeys.keyLastScore) 

    self.init(hightScore:hightScore, currentScore:currentScore, lastScore:lastScore) 
} 
İlgili konular