- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使游戏设置能够加载、保存自身并使其成为单例类。我的所有努力都失败了,XCode 询问我“无法调用没有参数的“Settings”类型的初始化程序”。我该如何解决这个问题?
这是代码:
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)
}
}
最佳答案
您正在尝试访问一个不接受当前类未实现的参数的构造函数。尝试重写 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) {
}
}
只是一个观点:需要传递参数才能配置的单例不再表现得像单例。
关于swift - 如何从继承 nsobject 和 nscoding 的类创建单例类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35341600/
SWIFT代码 print("1", NSObject() == NSObject()) print("2", ObjectIdentifier(NSObject()) == ObjectIdenti
保留在 NSObject 协议(protocol)中声明。 因此NSObject类和NSProxy类实现了它。 然而 NSProxy 和 NSObject 类都有一个分配。 为什么 alloc 没有在
我有一个已在App Store中发布的应用程序。大约有1%-2%的用户报告该应用程序崩溃了。这不是完全意外的行为,因此我要求提供崩溃日志。这是最后一个异常回溯(实际显示问题的部分): Last E
我有一个自定义 NSObject 类,可以调用 People,还有一个来自 CloudMade RMMarker.h 的名为 RMMarker 的类。 RMMarker 类有一个名为 data 的属性
这是崩溃报告,不知道为什么。我使用 AFHTTPRequestOperation setCompletionBlockWithSuccess:failure: 调用电话。在完成 block 中,我得到
应用程序有时会因 [NSObject(NSObject) doesNotRecognizeSelector:] 而崩溃,选择器为 [UIImageView setImage:]。 我通过设置异常断点捕
崩溃报告: 0 CoreFoundation!__exceptionPreprocess + 0x7c 1 libobjc.A.dylib!objc_exception_throw
我正在将我的整个项目从 Objective-C 转换为 Swift...在转换时我遇到了这个问题:“无法将‘Facility’类型的值转换为预期的参数类型“Facility!” 错误出现在我提到的最后
我在将数组[String]保存到核心数据时遇到问题。在核心数据中,我设置了字段“countires”的类型 - 可转换。 我通过以下方式添加数据: filters!.countries = selec
假设您有一个返回 NSDictionary 的方法。要构建字典,您可能需要创建一个 NSMutableDictionary。返回字典的不可变副本而不是仅返回可变字典是否有任何必要或优势? 例如 - (
在一个小型 RTS 项目中,我有一个按钮列表,每个按钮都分配有一个构建对象。有些建筑物非常通用,但其他建筑物则非常特殊,因此我将某些建筑物作为子类。 当我创建一个按钮列表时,每个按钮都有一个 PEHo
这个问题在这里已经有了答案: Objective-C: Property / instance variable in category (6 个答案) Objective-C: Instance
我实际上想知道如何从 NSObject 或任何其他类访问 encodeWithCoder、init 等方法而不继承 NSObject 类。 因为我读到如果我们从 NSObject 类继承,那么它在 S
我正致力于从 Objective-C 教程转换此委托(delegate)协议(protocol)函数,但在尝试通过使用下标返回基于键的值来访问字典中的值时遇到错误。我不太确定这里的错误是什么意思。任何
在我的 Swift 应用程序中,我有一个类: open class CustomCluster : NSObject { open var coordinate = CLLocationCoo
我有一个字典/数组,看起来像这样: var myArray: [[String:NSObject]] = [] let newItem = [ [
这很奇怪,我可以使用 Xcode 5 (5A1413) 在模拟器、iPhone 4S 和 iPhone 5 中运行我的应用程序而没有任何问题,但是当我将应用程序提交到 iTunes 商店时被拒绝并且此
我使用 NSKeyedArchiver.archivedDataWithRootObject(obj) 将对象转换为 NSData。 archivedDataWithRootObject(obj) 方
背景。 请考虑以下步骤: 1) 在 Xcode 中创建一个新的“单 View 应用程序”。 2)创建类NSObject+Extension.h和.m文件: // .h @interface NSObj
在 ARC 环境中遇到一些小问题。创建一个将 View 添加到父 View 的 NSObject - 它基本上是一个可以处理一些文本并显示它的“弹出类”。 在 View Controller 中它被实
我是一名优秀的程序员,十分优秀!