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

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

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

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

Related

Allow change in property only from one specific class

Suppose I have a class Participant which looks like this
Participant.h
#interface Participant : NSObject
#property(nonatomic, readonly) NSString *name;
#property(nonatomic, readonly) NSString *id;
#end
Here properties are readonly because I don't want anyone using this interface to change it
Besides ParticipantManager.h
What changes should I do in Participant class and how would I create ParticipantManager such that only ParticipantManager can change properties of Participant
More context
I get an event from react-native when value changes. to keep things in sync, I want my interface ParticipantManager to only change the values.
#import <React/RCTBridgeModule.h>
#import <React/RCTEventEmitter.h>
#interface ParticipantManager : RCTEventEmitter <RCTBridgeModule>
#end
^^ Above class should only be able to change properties of Participant class
For a user to change a value, he would call changeName method, which will send an event back to react-native where react-native would change value and send back to native code
what I have tried.
So, I thought about using class extensions concept but I am getting an error.
Here is what I did
I create Participant+Private.h which implements setName method
#import " Participant.h"
interface Participant()
- (void)setName:(NSString)name
- (void)setId:(NSString)name
#end
PS: I implemented setName and setId method in Participant.h
- (void)setName:(NSString)name {
_name = name;
}
but then when I am using it in my ParticipantManager.h, it is throwing error
No visible #interface for Participantdeclares the selector
setName
I am using it like this
#import "Participant+Private.h"
NSString* value = #"varun";
[[Participant sharedInstance] setName:value];
Can someone help me in fixing error?
Slightly detailed question here:
No visible #interface for Participant declares the selector setName
Objective-C doesn't provide that sort of reasoned privacy, e.g. "I am private to everyone except one other specific class which I hereby name". (Actually, I don't know any language that behaves that way, but that's not to say that there are no such languages of course.)
If this is a framework, you can use #package privacy to confine the privacy of something to other classes in the same framework.
Otherwise, Objective-C generally solves the visibility problem by importing headers, so if you put public accessors for Participant into a header and the only class that imports that header is ParticipantManager, then only ParticipantManager sees them.

Understanding why assign is breaking code in swift

I made an xcframework (in objective c) which is working fine in objective C but throws an error when using swift.
On debugging, I realized that it was breaking in swift because I was using assign.
I went through his answer: https://stackoverflow.com/a/4511004/10433835
where they say this
In most cases you'll want to use weak so you're not trying to access a deallocated object
I didn't quite get what assign does, but I don't think I am trying to access a deallocated object.
This is what I am doing
I have config with these properties
#import <Foundation/Foundation.h>
#interface Config : NSObject
#property(nonatomic, assign, readwrite) NSString *name;
#property(nonatomic, assign, readwrite) NSString *id;
#property(nonatomic, assign, readwrite) NSString *api;
#end
This is my Config.m file
#implementation Config
- (id)init {
if (self = [super init]) {
_api = #"https://api.xyz.in"
}
return self;
}
- (NSMutableDictionary *_Nonnull)configProperties {
if (!_name) {
[NSException raise:#"name" format:#"Room name cannot be null, please set room name"];
}
NSLog(#"Room name: %#", _name);
At this line it will throw Thread 1: EXC_BAD_ACCESS
NSLog(#" name: %#", _name);
if I remove assign, it won't throw any error
How am I calling it?
let config:Config = Config();
config.name = "varun_bindal";
let props = config.configProperties()
Can someone please explain me why using assign in swift is crashing my code? and why not using it doesn't.
You actually don't want a weak or assign set for that property. The object will be deallocated as soon as it is set. You want a strong reference because the object owns the property. weak will nil the pointer as soon as it's set. So checking the pointer will return nil. assign will keep the address of the pointer without keeping the actual memory set. Hence you're pointing to something that has been deallocated. ie: pointing to bad memory location. It's kind of a remnant of the old days really..
You can read here for more info on keywords:
https://exceptionshub.com/property-and-retain-assign-copy-nonatomic-in-objective-c.html
The answer is: It's complicated.
Swift takes care of object ownership for you. Your only responsibility is to create strong or weak variables to avoid retain cycles. When you stay in Swift, it takes care of everything else.
Life is more complicated in Objective-C, and more complicated still when you need to have Objective-C and Swift code interact.
In order for Swift and Objective-C to work correctly together, you have to declare the memory semantics of your Objective-C classes correctly.
When you declare an Objective-C property as assign, you're telling the compiler not to do any memory management on it. Your Swift code won't set up strong references, and won't be notified if the object has been deallocated. If the Objective-C code doesn't need it any more and releases it, and your Swift code tries to reference it, you will crash.
(As mentioned in comments, Objective-C's assign is equivalent to unowned in Swift.)
As Larme says, assign is ok for non-object scalar types, but not for objects.

Assign Enum to Variable in Objective-C

How can I assign an enum to a variable and access its value later? I thought this would be pretty simple, but every time I try to assign the enum value to a variable (no type mismatches or warnings in Xcode appear) my app crashes with an EXC_BAD_ACCESS error.
Here's how I setup my enum in my header file (BarTypes.h):
typedef enum {
BarStyleGlossy,
BarStyleMatte,
BarStyleFlat
} BarDisplayStyle;
No issues there (reading and using the values at least). However, when I create a variable that can store one of the enum values (BarStyleGlossy, BarStyleMatte, or BarStyleFlat) then try to set that variable, the app crashes. Here's how I setup and use the variable:
//Header
#property (nonatomic, assign, readwrite) BarDisplayStyle barViewDisplayStyle; //I've also tried just using (nonatomic) and I've also tried (nonatomic, assign)
//Implementation
#synthesize barViewDisplayStyle;
- (void)setupBarStyle:(BarDisplayStyle)displayStyle {
//This is where it crashes:
self.barViewDisplayStyle = displayStyle;
}
Why is it crashing here? How do I store the value of an enum in a variable? I think the issue has to do with a lack of understanding about enums on my end, however if I follow conventional variable setup and allocation, etc. this should work. Any ideas on what I'm doing wrong?
Please note that I'm new to enums, so my vocabulary here may be a bit mixed up (forgive me - and feel free to make an edit if you know what I'm trying to say).
I found a few references about enums across the web:
What is a typedef enum in Objective-C?
Using enum types as properties in Objective C
How to create global enum
How do I define and use an ENUM in Objective-C?
I also tried searching Apple's Developer site but only came up with results about types for Apple APIs (ex. Foundation, UIKit, etc.)
EDIT: Here's how I call the setupBarStyle method:
BarView *bar = [[BarView alloc] init];
[bar setupBarStyle:displayStyle];
Just in case anyone out there is still trying to figure out how to assign an enum value to an enum typed variable or property...
Here is an example using a property.
In the header file...
#interface elmTaskMeasurement : NSObject
typedef NS_ENUM(NSInteger, MeasurementType) {
D,
N,
T,
Y,
M
};
#property(nonatomic) MeasurementType MeasureType;
#end
In the file where the object is created...
elmTaskMeasurement *taskMeasurement = [[elmTaskMeasurement alloc] init];
taskMeasurement.MeasureType = (MeasurementType)N;
The method you implement is called setupBarStyle:, but you call setupBarShape: on the object.
I had this error myself but the error was caused by a different bug I off course create myself.
The setter of my property "myApplicationState" was as follows:
-(void)setApplicationStyle:(myApplicationStyle)applicationStyle{
self.applicationStyle = applicationStyle;
//some more code
}
Off course this would result in an endless loop because in the setter, the setting is called again, and again, and again.
It had to be:
-(void)setApplicationStyle:(myApplicationStyle)applicationStyle{
_applicationStyle = applicationStyle;
//some more code
}

Forward declare structs in Objective C

I'm writing an iOS app in which I have a model class that is going to initialize itself with an XMLElement I give to it.
I'm using TBXML for the XML part.
The header for the model class looks like this:
#interface CatalogItem : NSManagedObject
#property (nonatomic, retain) NSString * name;
#property (nonatomic, retain) NSManagedObject *group;
-(id)initWithXMLElement:(TBXMLElement*)element;
#end
Now instead of including the header in which TBXMLElement is defined, I'd like to forward declare it with: struct TBXMLElement before the class definition. I'm however getting an "Expected Type" error wich tells me my declaration isn't working. Is this not how I would got about this?
As I understand it, including header files in header files is bad practice. The compiler doesn't need to know the inner workings of TBXMLElement, just that it exists or will exist at compile time.
Forward declaration of structs are used all the time, but still involves importing a header. The motivation is to not allow developers to dip into the structure directly. I.e. look at CFString. It is implemented as a struct, but you can't touch the structure contents directly. Instead, there is a full API for manipulating the struct contents. This allows CFString's implementation details to change without breaking binary compatibility.
In your header (ideally the header that defines whatever API is associated with TBXMLElement*):
TBXMLElement.h:
typedef const struct TBLXMLElement *TBXMLElementRef;
extern TBXMLElementRef TBLXMLCreateElement();
... etc ...
Then, in the implementation file containing the implementation of the TBLXMLElementAPI:
TBXMElement.c (assuming it is a C file):
typedef struct __TBLXMLElement {
... struct members here ...
} TBLXMLElement;
TBXMLElementRef TBLXMLCreateElement()
{
return (TBXMLElementRef)malloc(sizeof(TBLXMLElement));
}
... etc ....

Purpose of Synthesize

I am using an iOS5 book to learn iOS programming.
#synthesize coolWord;
^synthesize is used for all properties in .m files
I heard that in iOS6 there is no need for synthesize, since it is automatically done for you. Is this true?
Does synthesize play any role for iOS6?
Thanks for the clarification. :)
#synthesize in objective-c just implements property setters and getters:
- (void)setCoolWord:(NSString *)coolWord {
_coolWord = coolWord;
}
- (NSString *)coolWord {
return _coolWord;
}
It is true with Xcode 4 that this is implemented for you (iOS6 requires Xcode 4). Technically it implements #synthesize coolWord = _coolWord (_coolWord is the instance variable and coolWord is the property).
To access these properties use self.coolWord both for setting self.coolWord = #"YEAH!"; and getting NSLog(#"%#", self.coolWord);
Also note, both the setter and getter can still be manually implemented. If you implement BOTH the setter and getter though you NEED to also manually include #synthesize coolWord = _coolWord; (no idea why this is).
Autosynthesis in iOS6 still requires #synthesize
to generate accessor methods for properties defined in a #protocol.
to generate a backing variable when you included your own accessors.
The second case can be verified like this:
#import <Foundation/Foundation.h>
#interface User : NSObject
#property (nonatomic, assign) NSInteger edad;
#end
#implementation User
#end
Type: clang -rewrite-objc main.m and check that the variable is generated. Now add accessors:
#implementation User
-(void)setEdad:(NSInteger)nuevaEdad {}
-(NSInteger)edad { return 0;}
#end
Type: clang -rewrite-objc main.m and check that the variable is NOT generated. So in order to use the backing variable from the accessors, you need to include the #synthesize.
It may be related to this:
Clang provides support for autosynthesis of declared properties. Using
this feature, clang provides default synthesis of those properties not
declared #dynamic and not having user provided backing getter and
setter methods.
I'm not sure how #synthesize relates to iOS6 but since Xcode 4.0, it's essentially been deprecated. Basically, you don't need it! Just use the #property declaration and behind the scenes, the compiler generates it for you.
Here's an example:
#property (strong, nonatomic) NSString *name;
/*Code generated in background, doesn't actually appear in your application*/
#synthesize name = _name;
- (NSString*)name
{
return _name;
}
- (void) setName:(NSString*)name
{
_name = name;
}
All that code is taken care of the complier for you. So if you have an applications that have #synthesize, it's time to do some cleanup.
You can view my similar question here which might help to clarify.
I believe that #synthesize directives are automatically inserted in the latest Obj-C compiler (the one that comes with iOS 6).
The point of #synthesize pre-iOS 6 is to automatically create getters & setters for instance variables so that [classInstance getCoolWord] and [classInstance setCoolWord:(NSString *)aCoolWord] are generated. Because they are declared with #property, you also get the convenience of dot syntax for the getter and setter.
hope this will help little more
yes previously we have to synthesis the property by using #synthesis now it done by IDE itself.
but we can use it like
// what IDE does internally
#synthesis name=_name;
we use _name to access particular property but now you want synthesis by some other way like
firstname you can do it like
#synthesis name= firstname
or just by name
#synthesis name=name
With automatic synthesis in iOS6, it is no longer necessary to specifically declare backing ivars or write the #synthesize statement. When the compiler finds a #property statement, it will do both on our behalf using the guidelines we’ve just reviewed. So all we need to do is declare a property like this:
#property (nonatomic, strong) NSString *abc;
and in iOS 6, #synthesize abc = _abc, will be added automatically at compile time.

Resources