The property in IOS Category and Protocol are you really going to do that?

Source: Internet
Author: User

First, property

Synthetic accessors:

@property format:

1 @property (modifier list) variable type variable name;

Before Xcode4.4:

@property causes the compiler to customize the generation of Set/get method declarations.

Implementation of @synthesize Auto-generated Set/get method

@synthesize also automatically generate private member variables

Xcode4.4 after:

Without having to write @synthesize, the compiler can give us the declaration and implementation of the Set/get method by @property, which generates the member variable by default: _propertyname

The member variables generated with @property are private.

When we want to change the default member variable name, @synthesize age = newName; '

If the subclass wants to access the member variables of the parent class,

1, through the Set/get method

2. Declared member variables displayed

* * Go to the chase * *

First, declare:

Category and protocol can add methods

@property keywords can be added in category and protocol

Second, about the property in protocol

When adding a property in protocol, it is actually declaring the getter and setter method, in the class that implements the protocol protocol , we have to manually add the instance variables ourselves, and we need to implement the Setter/getter method

Pseudo code:

Main.m

1 Student *student = [[Student alloc] init]; 2         3 @" Daddy Peng " ; 4         5 NSLog (@ "%@", student.name); 6         

Person.h

Set the protocol, with which the property

1 //2 //Person.h3 //Categories and agreements4 //5 //Created by Ma C on 16/5/19.6 //copyright©2016 year Pengsheng rime. All rights reserved.7 //8 9 #import<Foundation/Foundation.h>Ten  One @protocolPersondelegate <NSObject> A  -@property (nonatomic, copy) NSString *name; -  the @end -  - @interfacePerson:nsobject -  + @end

Student.h

Generating member variables, _name

1 //2 //PersonSon.h3 //Categories and agreements4 //5 //Created by Ma C on 16/5/19.6 //copyright©2016 year Pengsheng rime. All rights reserved.7 //8 9 #import<Foundation/Foundation.h>Ten #import "Person.h" One  A @interfaceStudent:nsobject <personDelegate> - { -     //* * * DECLARE member Variable * * * theNSString *_name; - } -  -  + @end

Student.m

Here are two ways to do this, one is to automatically implement Setter/gerter, a manual setter/gerter

1 //2 //personson.m3 //Categories and agreements4 //5 //Created by Ma C on 16/5/19.6 //copyright©2016 year Pengsheng rime. All rights reserved.7 //8 9 #import "Student.h"Ten  One @implementationStudent A  - //------------------Automatic----------------------// - //@synthesize the // - //during the compiler, let the compiler automatically generate the Getter/setter method.  - // - //custom masks automatically generate this method when there is a custom save or Fetch method +  - //automatic generation of Setter/getter methods +  A //@synthesize name; at  -  -  - //------------------Manual----------------------// -  - //@dynamic in // - //tells the compiler not to automatically generate Getter/setter methods to avoid warnings during compilation to // + //then implement the access method by yourself -  the //Set method that implements the property defined in person name *- (void) SetName: (NSString *) name { $_name =name;Panax Notoginseng      - } the  + //get method that implements the property defined in person name A-(NSString *) name { the     return_name; + } -  $ @end

Print log logs as:

Explain the basic function of successful implementation of property, and can implement assignment and value operation

Third, the property in category

When adding a property in the category, when adding getter and setter methods in @implentation, the category cannot add an instance variable

1) Use temporary global variables to override member variables

First declare: person does not have a name attribute

Main.m

Import Header file:

#import "Person+extension.h"

1 person *person = [[Person alloc] init]; 2         3 @" Peng " ; 4         5 NSLog (@ "%@", Person.name);

Person+extension.h

1 //2 //Person+extension.h3 //Categories and agreements4 //5 //Created by Ma C on 16/5/19.6 //copyright©2016 year Pengsheng rime. All rights reserved.7 //8 9 #import "Person.h"Ten  One @interfacePerson (Extension) A  -@property (nonatomic, strong) NSString *name; -  the @end

Person+extension.m

1 //2 //person+extension.m3 //Categories and agreements4 //5 //Created by Ma C on 16/5/19.6 //copyright©2016 year Pengsheng rime. All rights reserved.7 //8 9 #import "Person+extension.h"Ten #import<objc/runtime.h> One  A //* * * TEMP Variable * * * - StaticNSString *_name; -  the @implementationPerson (Extension) -  -- (void) SetName: (NSString *) name { -_name =name; + } -  +-(NSString *) name { A     return_name; at } - @end

2) Implementing member variables using Runtime association objects

In addition to person+extension.m the other is consistent with the first method

Runtime Person+extension.m

1 @implementationPerson (Extension)2 3- (void) SetName: (NSString *) name{4 Objc_setassociatedobject (self, @selector (name), name,objc_association_retain_nonatomic);5 }6 7-(NSString *) name{8     9NSString *n =Objc_getassociatedobject (self, @selector (name));Ten     returnN; One}

The two ways to print log logs are the same, as:

The property in IOS Category and Protocol are you really going to do that?

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.