Attribute method details declared in iOS Protocol and Category, ioscategory

Source: Internet
Author: User

Attribute method details declared in iOS Protocol and Category, ioscategory

I have always had a misunderstanding that the attribute cannot be declared in the form of @ property in the protocol and classification. Now let's make a summary:

In iOS, attributes can be declared in the form of @ property in the protocol and category, but only the corresponding setter/getter methods are declared in the protocol and category, no corresponding member variables are generated. Because only methods can be declared in the Protocol, only methods and corresponding implementations can be declared in the classification.
So how do I declare attributes with @ property in the protocol and then use member variables to directly access the attribute values in the classes that implement the protocol?

Protocol:

@protocol MyProtocol 
 
  @property (nonatomic,strong) NSString *myImage;@end
 

Implementation class:

@interface ViewController : UIViewController
 
  @end@implementation ViewController@synthesize myImage = _myImage;- (void)viewDidLoad {    [super viewDidLoad];    self.myImage = @"my string";    NSLog(@"%@,%@",_myImage,self.myImage);@end
 

The above method is mainly used@ Synthesize myImage = _ myImage in the Declaration above indicates that the myImage attribute is the _ myImage member variable synthesis accessors method. That is to say, the access method generated by the myImage attribute is setMyImage, And the setMyImage method is the access method of the _ myImage variable, which operates on the Variable _ myImage. Through this seemingly assign value operation, we can define the names of getter and setter which are different from the variable names in @ synthesize, so as to protect the variables from improper access. For example, you can write it as follows:

@ Interface ViewController: UIViewController
  
   
@ End @ implementation ViewController @ synthesize myImage = _ helloWorld; // Save the value of the set Method in _ helloWorld-(void) viewDidLoad {[super viewDidLoad]; self. myImage = @ "my string"; NSLog (@ "% @, % @", _ helloWorld, self. myImage); // self. myImage is the value of member Variable _ helloWorld; @ end
  

According to the above conclusion, can the @ synthesize be used in the Category to save the value of the member variable?
Unfortunately, the implementation in Category does not support @ synthesize for Attribute synthesis. The correct method is as follows:
Category:

@interface NSObject (Extension)@property (nonatomic,strong) NSString *myTitle;@end#import 
  
   @implementation NSObject (Extension)- (NSString *)myTitle {    return objc_getAssociatedObject(self, @selector(myTitle));   }- (void)setMyTitle:(NSString *)myTitle {    objc_setAssociatedObject(self, @selector(myTitle), myTitle,OBJC_ASSOCIATION_RETAIN);}
  

In this way, you can use. xx to obtain the access value in the implementation class:

@interface ViewController : UIViewController
  
   @end#import "NSObject+Extension.h"@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    NSObject *myObj = [[NSObject alloc] init];    myObj.myTitle = @"my title";    NSLog(@"%@",myObj.myTitle);@end
  

Supplement: differences between @ dynamic and @ synthesize:
In @ implementation, @ dynamic xxx tells the compiler that the setter and getter methods of the xxx attribute are generated by the developer.
@ Synthesize xxx = _ xxx; tells the compiler that the setter and getter methods of the xxx attribute are generated by the compiler and the member variables are synthesized using _ xxx.


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.