[Objective-C] Set and accessors of Object Attributes in OC

Source: Internet
Author: User

 

In object-oriented programming, creating an object will certainly contain some of its attributes, and then write the corresponding set for these attributes, get method (the seters and accessors we usually talk about)

1): Generally, the set and get methods of the attributes we write are as follows: Create an object, define declared attributes, and manually compile the setXXX () and getXXX () methods, as follows: Person object

1: Person. h

 

/// Person. h // PersonGetSet /// Created by hmjiangqq on 14-1-22. // Copyright (c) 2014 hmjiangqq. all rights reserved. // # import
 
  
@ Interface Person: NSObject {// declare a variable int myNumber;}-(int) myNumber;-(void) setMyNumber :( int) _ number;-(void) printf; @ end
 
2:Person.m
   
/// Person. m // PersonGetSet /// Created by hmjiangqq on 14-1-22. // Copyright (c) 2014 hmjiangqq. all rights reserved. // # import "Person. h "@ implementation Person-(int) myNumber {return myNumber;}-(void) setMyNumber :( int) _ number {myNumber = _ number;}-(void) printf {NSLog (@ "myNumber is % d \ n", myNumber) ;}@ end
This can also be done when there are few attributes. If an object has many attributes, We will manually write them one by one, which will waste development time and affect efficiency. therefore, the automatic configurator and accessors are available after OC2.0.
Next we will introduce the special settings and accessors in OC.
2: simplify the configurator and accessors. Pay attention to the two keywords @ property and @ synthesize.
  person.h
  
/// Person. h // PersonGetSet /// Created by hmjiangqq on 14-1-22. // Copyright (c) 2014 hmjiangqq. all rights reserved. // # import
 
  
@ Interface Person: NSObject {// declare a variable int myNumber;} // common get and set //-(int) myNumber; //-(void) setMyNumber :( int) _ number; @ property (nonatomic) int myNumer; // after such declaration, the system will dynamically create-(void) printf; @ end
 
Person. m
 
/// Person. m // PersonGetSet /// Created by hmjiangqq on 14-1-22. // Copyright (c) 2014 hmjiangqq. all rights reserved. // # import "Person. h "@ implementation Person // common get and set implementations //-(int) myNumber {// return myNumber; //} //-(void) setMyNumber :( int) _ number {// myNumber = _ number; //} @ synthesize myNumer;-(void) printf {NSLog (@ "myNumber is % d \ n", myNumber );} @ end
2) attributes that can be filled in @ property () brackets:
Readwrite: Default
Readonly: does read-only mean there is a set Method in it?
Assign: by default, the reference count is not increased
Retain: The reference count is increased by 1.
Atomicity: actiomic default
Non-atomicity: nonatomic


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.