IOS Development Learning SUMMARY objective-c object-oriented -- Synthesis access method and point syntax

Source: Internet
Author: User

IOS Development Learning SUMMARY objective-c object-oriented -- Synthesis access method and point syntax
Synthetic access method

We have introduced the setter and getter methods for member variables. However, if a class contains many member variables, it is inconvenient.
Objective-c automatically combines the setter method and getter method from OC 2.0. In addition, if developers need to control the implementation of a setter method and getter method, they can provide their own setter method and getter method, the setter and getter methods provided by developers will overwrite the corresponding methods automatically merged by the system.

To enable the system to automatically synthesize setter and getter methods, use the @ property command in the class interface section to define properties. -- @ Property command is placed before the property definition and directly placed between @ interface and @ end. In the class implementation section, use the @ synthesize command to declare this attribute.
After the preceding two steps are used, the setter and getter methods are merged. A member variable with the same name as the getter method is automatically defined in the class implementation section. This part can see a previous blog post [IOS development learning Summary-OC-11]★Objective-c object-oriented-encapsulation and access control operator
If a member variable is defined for a class and corresponding setter and getter methods are provided, a property is defined ).
In the code specification after Xcode4.0, we recommend that you define the member variable name to start with the following line. For example, the template code of Xcode contains the following code: @synthesize window=_window;The function of this Code is to tell the system that the member variable corresponding to the merged property is _ window, rather than window.
@ Synthesize syntax format: @ Synthesize property name = member variable name;Or @ Synthesize property name;
@ Synthesize if no specified member variable name exists, the member variable name is the same as the merged getter method by default.
Demonstrate the usage of the synthetic access method. Sample Code:
FKUser. h file:
# Import
  
   
@ Interface FKUser: NSObject // use @ property to define three properties @ property (nonatomic) NSString * name; @ property NSString * pass; @ property NSDate * birth; @ end
  

FKUser. m file:

# Import FKUser. h @ implementation FKUser // synthesize the setter and getter methods for the three properties. // specify the name of the member variable corresponding to the underlying name property as _ name @ synthesize name = _ name; @ synthesize pass; @ synthesize birth; // implement custom setName: method, add your own control logic-(void) setName :( NSString *) name {self-> _ name = [NSString stringWithFormat: ++ % @, name] ;}@ end

FKUserTest. m file:

# Import FKUser. hint main (int argc, char * argv []) {@ autoreleasepool {// create the FKUser object FKUser * user = [[FKUser alloc] init]; // call the setter method to modify the value of the user member variable [user setName: @ admin]; [user setPass: @ 1234]; [user setBirth: [NSDate date]; // access NSLog (@ Administrator Account: % @, password: % @, birthday: % @, [user name], [user pass], [user birth]) ;}}

The program uses @ property, @ synthesize to synthesize setter. After the getter method is used, the program can use the setter and getter methods to access the value of the member variable.

Assign, a special indicator used to define @ property

Assign:The attribute is simply assigned a value without changing the reference count of the assigned value. It is applicable to basic types such as NSInteger and various C data types such as short, float, and struct. So the question is,

Why are there no recycling issues for basic types such as NSInteger, and various C data types such as short, float, and struct?

The Garbage Processing Mechanism in iOS is based on the number of indexes (reference count) of an object. When it is 0, it indicates that this object is not used, and the object will be cleared, the basic data type does not belong to an object. It is created and used in the memory and is cleared when it exceeds the corresponding method body. Therefore, the garbage processing mechanism is not required, there is no need to record the index value (count) and there is no recycling problem. So use assgin.

Atomic and nonatomic

Atomic (nonatomic ):Specifies whether the integrated access method is an atomic operation. The default value is atomic.
If atomic is used, the combined access method isThread Security-- When a thread enters the storage and the method body is retrieved, other threads cannot enter the storage and the method is retrieved. This prevents multiple threads from damaging the data integrity of the object.

Copy

Copy:When the copy indicator is usedSetter MethodWhen a value is assigned to a member variable, the assigned object is copied to a copy and then assigned to the member variable.Copy will reduce the reference count of the object referenced by the original member variable by 1.

When should I use copy?

When the member variable type is mutable or its subclass is mutable, the assigned object may be modified after the value is assigned, if the program does not need this modification to affect the value of the member variable set by the setter method, use the copy indicator.
Example program:
FKBook. h

# Import
  
   
@ Interface FKBook: NSObject // use @ property to define 1 property @ property (nonatomic) NSString * name; @ end
  

FKBook. m

#import FKBook.h@implementation FKBook@synthesize name;@end

FKBookTest. m

# Import FKBook. hint main (int argc, char * argv []) {@ autoreleasepool {FKBook * book = [[FKBook alloc] init]; // NSMutableString is a subclass of NSString NSMutableString * str = [NSMutableString stringWithString: @ iOS handout]; // assign a value to the name attribute of book [book setName: str]; // output the NSLog (@ book name: % @, [book name]) of the book; // modify the str string [str appendString: @ is a good iOS learning book]; // the following code will show that the name attribute of the book will also be changed to NSLog (@ book's name is: % @, [book name]) ;}}

After compiling and running the program, the output content is:
Book name: iOS handout
The book name is: iOS handout is a good iOS learning book.

If we do not want the name attribute of FKBook to change with the NSMutableString object, we can add the copy indicator to the line defining the name attribute,@property (nonatomic,copy) NSString* name;

Setter & getter

Used to specify the custom method name for the merging setter and getter methods.
Example:getter=abcSpecify the getter method name as abc,setter=xyz:The setter method is named xyz.Note:--Do not forget to include a colon in the setter method because it must contain parameters.
Example program:
FKItem. h

# Import
  
   
@ Interface FKItem: NSObject // use @ property to define one property and specify the custom getter and setter method names @ property (assign, nonatomic, getter = wawa, setter = nana :) int price; @ end
  

FKItem. m

#import FKItem.h@implementation FKItem@synthesize price;@end

FKItemTest. m

# Import FKItem. hint main (int argc, char * argv []) {@ autoreleasepool {FKItem * item = [[FKItem alloc] init]; // set the price attribute [item nana: 30]; // access the price attribute NSLog (@ item's price is: % d, [item wawa]);}

SetPrice: the method and the obtained price method do not exist. Instead, they are nana and wawa.

Readonly & readwrite

Readonly indicates that the system only synthesize the getter method instead of the setter method. Readwrite is the default value. Both methods are merged.

Retain

When you use the retain indicator to define an attribute, When you assign an object to this attribute, the reference count of the originally referenced object of this attribute is-1, and the reference count of the assigned object is + 1.
Note:Generally, less retain is used when ARC is started. However, retain is useful when ARC is not enabled.
Example program: (The following program closes ARC)
FKWin. h

# Import
  
   
@ Interface FKWin: NSObject // use @ property to define one property @ property (nonatomic, retain) NSDate * date; @ end
  

FKWin. m

#import FKWin.h@implementation FKWin@synthesize date;@end

FKWinTest. m

# Import FKWin. hint main (int argc, char * argv []) {FKWin * win = [[FKWin alloc] init]; NSDate * date = [[NSDate alloc] init]; // when the value is assigned for the first time, the reference count of date is 1 NSLog (reference count of @ date is: % ld, date. retainCount); // The reference count of date + 1 [win setDate: date] When values are assigned due to the use of the retain indicator; // The reference count output below is 2 NSLog (@ [win date] reference count: % ld, [win date]. retainCount); // release date reference count, date reference count-1 [date release]; // The reference count output below is 1 NSLog (@ [win date] reference count: % ld, [win date]. retainCount );}
Point syntax

Objective-c allows you to use simplified dot syntax to access attributes and assign values to attributes. In essence, it still calls the getter and setter methods.
Example program:
FKCard. h

# Import
  
   
@ Interface FKCard: NSObject // use @ property to define two properties @ property (nonatomic, copy) NSString * flower; @ property (nonatomic, copy) NSString * value; @ end
  

FKCard. m

#import FKCard.h@implementation FKCard@synthesize flower;@synthesize value;@end

FKCardTest. m

# Import FKCard. hint main (int argc, char * argv []) {@ autoreleasepool {FKCard * card = [[FKCard alloc] init]; // assign a value to the attribute using the dot syntax card. flower = @♠; Card. value = @ A; // use the dot syntax to access the NSLog of the attribute value (@ my poker cards: % @, card. flower, card. value );}}

 

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.