Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • What is use of synthesize?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 207
    Comment on it

    The @property and @synthesize keywords simply automate the creation of getter and setter methods,

    @interface MyClass : NSObject
    @property int value;
    @end
     
    @implementation MyClass
    @synthesize value;
    @end
    

    The @property keyword declares the property in the @interface section, and if necessary, adds an instance variable of the same name and type to the class behind the scenes. The @synthesize statement then causes the compiler generates both the getter and setter and (if not set to be a readonly property) the setter methods for that property.
    You can still use the same code to access the value as before, and you can use dot notation as an alternative (and more natural) way to access properties of an Objective-C class:

    MyClass* instance = [[MyClass alloc] init];
    int val2 = instance.value + 10;
    instance.value = val2;
     
    // this still works and does the exact same thing:
    int val = [instance value] + 10;
    [instance setValue:val];
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: