gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 01:56:06 26 4
gpt4 key购买 nike

我正在尝试使用 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com