试图在SpriteKit编辑器中使用GKComponent子类静默地在保存时崩溃Xcode

问题描述 投票:1回答:1

我正在尝试使用Xcode中的场景编辑器使用GameplayKit和SpriteKit。我是SpriteKit的新手,之前还没有使用过NSSecureEncoding。坦白说,相当新鲜。

我有一个基本的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?(编码器:)失败并返回一个空对象,这导致错误。

此外,如果我删除String属性,这将完美无缺。我不知道导致init的原因是什么?失败...

无论如何,我可能会离开基地,但我很确定我错过了一些简单的东西,看起来很愚蠢,但我无法理解。

谢谢你的帮助。

ios swift xcode gameplay-kit nssecurecoding
1个回答
0
投票

使用NSString而不是String

© www.soinside.com 2019 - 2024. All rights reserved.