【问题标题】:How to make singleton class from class, which inherits from nsobject and nscoding?如何从继承nsobject和nscoding的类中创建单例类?
【发布时间】:2016-02-11 14:11:05
【问题描述】:

我正在尝试使游戏设置加载、保存并使其成为单例类。我所有的努力都以失败告终,XCode 询问我“无法为没有参数的“设置”类型调用初始化程序”。我该如何解决这个问题?

这是代码:

class Settings: NSObject, NSCoding {

static let sharedInstance = Settings()

var currentLevel: Int
var positionOfPlayer: [Int]?
var sounds: Bool
var shape: String
var completedLevels: [Int: Bool]

init?(currentLevel: Int, positionOfPlayer: [Int]?, sounds: Bool, shape: String, completedLevels: [Int: Bool]) {
    self.currentLevel = currentLevel
    self.sounds = sounds
    self.shape = shape
    self.completedLevels = completedLevels

    if let position = positionOfPlayer as [Int]? {
        self.positionOfPlayer = position
    }

    super.init()
}

required convenience init?(coder aDecoder: NSCoder) {
    let currentLevel = aDecoder.decodeObjectForKey(SettingNames.nameOfCurrentLevel) as? Int
    let completedLevels = aDecoder.decodeObjectForKey(SettingNames.nameOfCompletedLevels) as? [Int: Bool]
    let positionOfPlayer = aDecoder.decodeObjectForKey(SettingNames.positionOfPlayerOnCurrentLevel) as? [Int]
    let sounds = aDecoder.decodeObjectForKey(SettingNames.nameOfSounds) as? Bool
    let shape = aDecoder.decodeObjectForKey(SettingNames.nameOfShapes) as? String
    self.init(currentLevel: currentLevel!, positionOfPlayer: positionOfPlayer, sounds: sounds!, shape: shape!, completedLevels: completedLevels!)
}

func encodeWithCoder(aCoder: NSCoder) {
    aCoder.encodeObject(currentLevel, forKey: SettingNames.nameOfCurrentLevel)
    aCoder.encodeObject(completedLevels, forKey: SettingNames.nameOfCompletedLevels)
    if let position = positionOfPlayer as [Int]? {
        // If game canceled or ended during playing, it saves the current player position.
        // Next time, when player open the game, it will continue
        aCoder.encodeObject(position, forKey: SettingNames.positionOfPlayerOnCurrentLevel)
    }
    aCoder.encodeBool(sounds, forKey: SettingNames.nameOfSounds)
    aCoder.encodeObject(shape, forKey: SettingNames.nameOfShapes)
}

}

【问题讨论】:

    标签: swift sprite-kit singleton


    【解决方案1】:

    您正在尝试访问不接受当前类未实现的参数的构造函数。尝试覆盖 init 方法,这应该可以消除错误。

    override init(){
       // some code
    }
    

    这是我尝试过的完整示例:

    import Foundation
    
      class Settings : NSObject, NSCoding {
    
      static let sharedInstance = Settings()
    
      var a: String?
      var b: String?
    
        convenience init(a: String, b: String){
          self.init()
          self.a = a
          self.b = b
        }
    
      override init(){
    
      }
    
      required init?(coder aDecoder: NSCoder) {
    
      }
    
      func encodeWithCoder(aCoder: NSCoder) {
    
      }
    
    }
    

    只是一个观点:需要您传递参数才能对其进行配置的单例不再像单例那样运行。

    【讨论】:

    • 我按照你的建议做了!谢谢阿明!还有一件事,我以正确的方式做了我的单身人士。所以在这里我有一个问题。我想首先在视图控制器中加载这个单例类。但我不想要设置类的任何属性,因为这个类是单例的。那么你对这个小问题有什么决定吗?谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-08
    • 1970-01-01
    • 2011-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多