objective c - Confused About Getter and Setter -


I am currently learning Obj-C for the purpose of iOS programming, but there are some concepts which I am confused about . Specifically, gater and seater accessories

I'm reading many different things about them - some are saying that when you use @property, they are automatically created and set them manually There is no requirement, others are asking you to use @ synthesis.

Please see the comments on the code below. I'm really confused by the need for manually calling code and method manually.

  // SomeClass.h #import & lt; Foundation / Foundation. H & gt; @ Interface some class: NSObject @property (nonatomic, strong) NSString * firstName; - (NSString *) firstName; @ And // SomeClass.m #import "SomeClass.h" @ Implementation SomeClass // Do I need the code given below or are they self-produced? // * ************************************************************************************************ **************************************************************************************************** *** - (NSString *) firstName {return _firstName; } - (zero) setFirstname: (NSString *) newValue {_firstName = newValue; } // *********************************************************************************************** **************************************************************************************************** **** @ end int main () {@autoreleasepool {SomeClass * aclass = [[some class alot] init]; // How would you set the first name? Aclass.firstName = @ "Richard"; // Set this call first name? // or [aclass setfirst_name: @ "Richard"]; } Return 0; }   

Some of the things you are reading are old information @ synthesis (and It can still be used), but during compilation it is now automatically added for you (and you should usually use that option).

You must enter (NSString *) firstName; @interface, because the @ property definition is already defined.

// Do I need the code below or is it automatically created?

No, you do not need it, it is automatically generated for you. If you want to do something special, you will apply the methods yourself. Generally, auto-generated versions are great because you specify how the @property definition should be implemented in the (nonatomic, strong) and it has been cared for you if If you apply your own methods, then you are breaking it (hence the @ property definition can be a lie ...).

aclass.firstName = @ "Richard"; // Set this call first name?

Yes it does compiler actually changes into:

  [aclass setFirstName: @ "Richard"];  

For you.


Comments