You must declare a public instance variable in Objective-C

I am trying to declare some instance variables for a custom button class in Objective-C (for iOS):

@interface PatientIDButton : UIButton { NSUInteger patientID; NSString * patientName; } @end 

However, they are now private, and I need them to be available to other classes. I think I could make access functions for them, but how would I make the variables open by themselves?

+7
source share
1 answer

To make instance variables public, use the @public keyword, for example:

 @interface PatientIDButton : UIButton { // we need 'class' level variables @public NSUInteger patientID; } @end 

Of course, you need to keep in mind all the standard precautions when exposing "raw" variables for public access: you would be better off with properties because you would retain the flexibility to change their implementation at a later time.

Finally, you need to remember that accessing public variables requires dereferencing - either with an asterisk or with an operator -> :

 PatientIDButton *btn = ... btn->patientID = 123; // dot '.' is not going to work here. 
+14
source

Source: https://habr.com/ru/post/1490093/


All Articles