Trying to use GKComponent subclass in SpriteKit editor silently crashes Xcode on save





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















I am trying to work with GameplayKit and SpriteKit using the scene editor in Xcode. I am new to SpriteKit and also have not used NSSecureEncoding before. Pretty new at swift to be honest.



I have a basic GKComponent subclass with one integer one string property implemented like this.



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)
}


}


Every time I add this component to a node, Xcode crashes silently when the scene is saved (autosave or on purpose). The backtrace in the Xcode crash log file shows an 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)


Also new at reading backtraces but it seems to me that the init?(coder:) fails and returns a null object, which is causing the error.



Furthermore, if i remove the String property, this works flawlessly. I don't know what is causing the init? to fail...



Anyway I could be way off base but I'm quite sure I am missing something simple and will look stupid, but I just can't figure it out.



Thanks for any help.










share|improve this question





























    1















    I am trying to work with GameplayKit and SpriteKit using the scene editor in Xcode. I am new to SpriteKit and also have not used NSSecureEncoding before. Pretty new at swift to be honest.



    I have a basic GKComponent subclass with one integer one string property implemented like this.



    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)
    }


    }


    Every time I add this component to a node, Xcode crashes silently when the scene is saved (autosave or on purpose). The backtrace in the Xcode crash log file shows an 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)


    Also new at reading backtraces but it seems to me that the init?(coder:) fails and returns a null object, which is causing the error.



    Furthermore, if i remove the String property, this works flawlessly. I don't know what is causing the init? to fail...



    Anyway I could be way off base but I'm quite sure I am missing something simple and will look stupid, but I just can't figure it out.



    Thanks for any help.










    share|improve this question

























      1












      1








      1








      I am trying to work with GameplayKit and SpriteKit using the scene editor in Xcode. I am new to SpriteKit and also have not used NSSecureEncoding before. Pretty new at swift to be honest.



      I have a basic GKComponent subclass with one integer one string property implemented like this.



      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)
      }


      }


      Every time I add this component to a node, Xcode crashes silently when the scene is saved (autosave or on purpose). The backtrace in the Xcode crash log file shows an 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)


      Also new at reading backtraces but it seems to me that the init?(coder:) fails and returns a null object, which is causing the error.



      Furthermore, if i remove the String property, this works flawlessly. I don't know what is causing the init? to fail...



      Anyway I could be way off base but I'm quite sure I am missing something simple and will look stupid, but I just can't figure it out.



      Thanks for any help.










      share|improve this question














      I am trying to work with GameplayKit and SpriteKit using the scene editor in Xcode. I am new to SpriteKit and also have not used NSSecureEncoding before. Pretty new at swift to be honest.



      I have a basic GKComponent subclass with one integer one string property implemented like this.



      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)
      }


      }


      Every time I add this component to a node, Xcode crashes silently when the scene is saved (autosave or on purpose). The backtrace in the Xcode crash log file shows an 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)


      Also new at reading backtraces but it seems to me that the init?(coder:) fails and returns a null object, which is causing the error.



      Furthermore, if i remove the String property, this works flawlessly. I don't know what is causing the init? to fail...



      Anyway I could be way off base but I'm quite sure I am missing something simple and will look stupid, but I just can't figure it out.



      Thanks for any help.







      ios swift xcode gameplay-kit nssecurecoding






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 24 '18 at 2:52









      Mike BeaumontMike Beaumont

      4113




      4113
























          0






          active

          oldest

          votes












          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53454773%2ftrying-to-use-gkcomponent-subclass-in-spritekit-editor-silently-crashes-xcode-on%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53454773%2ftrying-to-use-gkcomponent-subclass-in-spritekit-editor-silently-crashes-xcode-on%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          Xamarin.form Move up view when keyboard appear

          Post-Redirect-Get with Spring WebFlux and Thymeleaf

          Anylogic : not able to use stopDelay()