Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Accessor Methods in iPhone

    • 0
    • 1
    • 0
    • 0
    • 1
    • 0
    • 0
    • 0
    • 659
    Comment on it

    accessor methods is used to access or set an objects properties:

    Syntax-

    NSString *firstName = [somePerson firstName];
    
    [somePerson setFirstName:@"Johnny"];
    

    These accessor methods are by default and automatically synthesized by the compiler, therefore, now you only have to declare the property using @property in the class interface.

    The synthesized methods follow specific naming conventions mentioned below:
    The method used to access the value (the getter method) has the same name as the property.The getter method for a property called firstName will also be called firstName.
    The method used to set the value (the setter method) starts with the word set and then uses the capitalized property name.
    The setter method for a property called firstName will be called setFirstName:.
    If you dont want to allow a property to be changed via a setter method, you can add an attribute to a property declaration to specify that it should be readonly:

    Syntax-

    @property (readonly) NSString *fullName;
    

    As well as to show other objects how they are supposed to interact with the property, the attributes tell the compiler how to synthesize the relevant accessor methods. hence in this case, the compiler will synthesize a fullName getter method, but not a setFullName: method.

    Reference

    https://developer.apple.com/library/ios

 1 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: