Johan Dahl
Johan Dahl

Reputation: 1

How to create a new object and assign properties in Objective-C?

I'm trying to bridge an Objective C SDK with React Native and I'm having some trouble. I have a Subclass of NSObject and I'm trying to set some property values but I can't get it to work.

I have tried to change the property in the header, and in the imp file with out any difference.

PrinterSDK.h (which has libPrinterSDK.a)

@interface Printer : NSObject

@property (nonatomic, readonly) NSString* name;
@property (nonatomic, readonly) NSString* UUIDString;

@end

RNPosPrint.m

@interface Printer ()
@property (readwrite) NSString* name;
@property (readwrite) NSString* UUIDString;
@end

RCT_EXPORT_METHOD(printTestPaper:(NSString*)name:(NSString*)uuid)
{

  Printer* printer = [[Printer alloc] init];   
  printer.name = name;

}

But I keep facing issue with the setter for some reason I can't figure out.

ExceptionsManager.js:94 Exception '-[Printer setPrinterName:]: unrecognized selector sent to instance 0x13fd25b90' was thrown while invoking printTestPaper on target RNPosPrint with params (
    "Test Printer",
    "XXX-XXX-XXX"
)

Upvotes: 0

Views: 433

Answers (2)

Johan Dahl
Johan Dahl

Reputation: 1

Since it's an interface from statically linked library it is simply not possible to extend or manipulate. Not without tempering with the compiler.

Upvotes: 0

CRD
CRD

Reputation: 53010

You do not report the names of your .h and .m files or what else is in the .m – e.g. @implementation of Printer? The class printTestPaper belongs to? Without details like this it is difficult for anyone to help you, you need to help people help you.

That said some points that may help you:

  • The @interface Printer () where you open up the properties to be writeable should be in the your Printer.m file – in general do not try to open up access to a type's properties from outside the type's implementation, it is both bad design and may not work as you hope (as you just found out).
  • The code to support a @property is generated by the compiler when it compiles the @implementation, @interface's themselves produce no executable code – they describe the accessible parts of the @implementation.
  • setter=<name> provides a different name for the auto-created property setter function. While a method <name> will be created to set the property using dot syntax the properties name is still used, e.g. in your case printer.name = ... is still used even with the setter=setPrinterName:. You can call the auto-created method using standard method syntax, that failed in your case for the reasons above.
  • Using setter=<name> or getter=<name> are really advanced features and you probably will never need to use them – when you do need to use them you will know! Just avoid them till then.
  • If you wish to provide a method which creates the object and sets properties then do this in the type's implementation. The usual way of doing this is to provide an init method that does this, e.g. in this case it might be - initWithName:(NSString *)printerName { ... }, or an equivalent class method which does the allocation and sets the parameters, e.g. in this case it might be + newWithName:(NSString *)printerName { ... }.

HTH

Upvotes: 1

Related Questions