-1

I'm working on a obj-c project and I want to implement the NSCoding protocol for a class, but I can't get it right. The code looks like this:

#include <Foundation/Foundation.h>

class Object: NSObject, NSCoding {
    //Somecode
}

And I get the error: "Base specifier must name a class" and "Expected class name". What am I doing wrong?

Cristik
  • 30,989
  • 25
  • 91
  • 127
user3612623
  • 245
  • 3
  • 13
  • 3
    Is this swift or Objective-c? If it is swift you shouldn't have the include, if it is objective-c is this a header file or implementation file? – Dan F Jan 24 '16 at 14:59
  • It's Objective-C and it's a header file. The code before was "class Object { //Somecode}" without the ": NSObject, NSCoding" and it worked well. – user3612623 Jan 24 '16 at 16:19
  • 1
    The syntax for adopting a protocol is `MyClass : NSObject `, what you wrote is how to declare inheritance, and ObjC doesn't do multiple-inheritance, which is why you get an error. – Avi Jan 24 '16 at 16:35
  • I also tried writing it like that and then I get the errors: "Unknown template name 'NSObject'" and "Use of undeclared identifier 'NSCoding'" – user3612623 Jan 24 '16 at 16:39

1 Answers1

2

You are declaring a C++ class, not a Objective-C one, and you cannot have inheritance from one language to another. You'll need to change your class declaration to something like

@interface Object: NSObject <NSCoding> {
    // iVar declarations
}

// method and property declarations

@end

Although not sure how much it will help if your class already has defined C++ methods, as you'll need to port those one to Objective-C definitions.

I highly recommend you go through the link I posted in my comments, and read Apple's documentation on working with classes and objects. This will help you with the transition.

Cristik
  • 30,989
  • 25
  • 91
  • 127