ios - 尝试在 SpriteKit 编辑器中使用 GKComponent 子类会在保存时静默崩溃 Xcode

标签 ios swift xcode gameplay-kit nssecurecoding

我正在尝试使用 Xcode 中的场景编辑器来处理 GameplayKit 和 SpriteKit。我是 SpriteKit 的新手,之前也没有使用过 NSSecureEncoding。老实说,Swift 还是个新手。

我有一个基本的 GKComponent 子类,其中一个整数一个字符串属性是这样实现的。

import Foundation
import GameplayKit

@objc
class BasicComponent: GKComponent {

    @GKInspectable var number: Int = 0
    @GKInspectable var text: String = " "

    struct CoderKeys {
        static let stringKey = "StringKey"
        static let integerKey = "IntegerKey"
    }

    override static var supportsSecureCoding: Bool  {
        return true
    }

    required convenience init?(coder aDecoder: NSCoder) {
        self.init(coder: aDecoder)
        if let num = aDecoder.decodeObject(of: NSNumber.self, forKey: CoderKeys.integerKey) { self.number = num.intValue }
        if let tex = aDecoder.decodeObject(of: NSString.self, forKey: CoderKeys.stringKey) { self.text = tex as String }
    }

    override func encode(with aCoder: NSCoder) {
        super.encode(with: aCoder)
        aCoder.encode(self.number as NSNumber, forKey: CoderKeys.integerKey)
        aCoder.encode(self.text as NSString, forKey: CoderKeys.stringKey)
    }


}

每次我将此组件添加到节点时,Xcode 在保存场景(自动保存或有意保存)时会无提示地崩溃。 Xcode 崩溃日志文件中的回溯显示 NSInvalidArgumentException:

UNCAUGHT EXCEPTION (NSInvalidArgumentException): *** -[__NSPlaceholderArray initWithObjects:count:]: attempt to insert nil object from objects[3]
UserInfo: (null)
Hints:

Backtrace:
0   __exceptionPreprocess (in CoreFoundation)
1   DVTFailureHintExceptionPreprocessor (in DVTFoundation)
2   objc_exception_throw (in libobjc.A.dylib)
3   -[CFPrefsConfigurationFileSource initWithConfigurationPropertyList:containingPreferences:] (in CoreFoundation)
4   -[__NSPlaceholderArray initWithObjects:count:] (in CoreFoundation)
5   +[NSArray arrayWithObjects:count:] (in CoreFoundation)
6   -[GTFSceneCustomComponentProperty initWithCoder:] (in GameToolsFoundation)
7   _decodeObjectBinary (in Foundation)
8   -[NSKeyedUnarchiver _decodeArrayOfObjectsForKey:] (in Foundation)
9   -[NSDictionary(NSDictionary) initWithCoder:] (in Foundation)
10   _decodeObjectBinary (in Foundation)
11   _decodeObject (in Foundation)
12   -[NSKeyedUnarchiver decodeObjectForKey:] (in Foundation)
13   -[NSKeyedUnarchiver decodeObjectOfClasses:forKey:] (in Foundation)
14   -[GTFSceneCustomComponent initWithCoder:] (in GameToolsFoundation)
15   _decodeObjectBinary (in Foundation)
16   -[NSKeyedUnarchiver _decodeArrayOfObjectsForKey:] (in Foundation)
17   -[NSArray(NSArray) initWithCoder:] (in Foundation)
18   _decodeObjectBinary (in Foundation)
19   _decodeObject (in Foundation)
20   -[NSKeyedUnarchiver decodeObjectForKey:] (in Foundation)

在阅读回溯方面也是新的,但在我看来,init?(coder:) 失败并返回一个空对象,这导致了错误。

此外,如果我删除 String 属性,这将完美无缺地工作。我不知道是什么导致了初始化?失败...

无论如何,我可能会偏离基础,但我很确定我遗漏了一些简单的东西并且看起来很愚蠢,但我就是想不通。

感谢您的帮助。

最佳答案

使用 NSString 而不是 String

关于ios - 尝试在 SpriteKit 编辑器中使用 GKComponent 子类会在保存时静默崩溃 Xcode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53454773/

相关文章:

ios - iPad 纹理加载差异(32 位与 64 位)

iOS:如何生成随机柔和的颜色?

ios - 从核心数据中获取日期,然后获取并显示在tableViewCell中

ios - 将数据保存到 iOS 的最佳方式?

xcode - 缺少自动完成和文档

ios - iOS 中的核心图

ios - 以编程方式使用布局 anchor 创建 subview

ios - swift 中推送到新 View Controller 的方法

ios - PBXCp 错误.. Storyboard : No such file or directory

ios - 带有动画的 Collection View 滚动导致错误的图像位置