iOS adds attributes to a category

Source: Internet
Author: User

Sometimes, we want to add a property to someone else's class. This property will only be used in its own module and useless elsewhere. So now use two options:

1, add a property directly to the original class

2, write a classification, add this attribute in the classification.

The similarities and differences between the two scenarios:

  same as: All can achieve the requirement to add an attribute to an existing class.

  different :

The first scenario benefits: simple to implement. Cons: Destroying the encapsulation of existing classes will create confusion for subsequent developers. Conclusion: Stupid method

The second scenario is a bit: able to completely separate from the existing classes, maintaining the modularity of independence. Seemingly no shortcomings, if you must find a disadvantage, it is a word "difficult", because to use a tall on the operating mechanism to achieve the purpose. Conclusion: High-end atmosphere on the grade!

Specific implementations: (Skip the first scenario and directly describe the second scenario)

For example, there is now a person class with two properties in the class: Name, and age. There are two of these classes that are already in demand. However, in another separate module, it is required to add an address property that does not affect the previous module.

The existing classes are as follows:

. h

#import <Foundation/Foundation.h>

@interface Person:nsobject

@property (nonatomic, copy) nsstring* name;

@property (nonatomic, assign) int age;

@end

. m

#import "Person.h"

@implementation person

@end

The classification is implemented as follows:

. h

#import "Person.h"

@interface person (Address)

@property (nonatomic, copy) nsstring* address;

@end

. m

#import "Person+address.h"

@implementation Person (Address)

@end

It looks like it's going to be here. Go back to MAIN.M and try. Shixian:

person* p = [[Person alloc] init];

P.address = @ "Celestial";

NSLog (@ "address-->%@", p.address);

So simple!!! Altogether wood has an error. Compile, the problem is, log is:

-[person setaddress:]: Unrecognized selector sent to instance 0x100110e50 .....

Terminating app due to uncaught exception ' nsinvalidargumentexception ', Reason: '-[person setaddress:]: Unrecognized s Elector sent to instance 0x100110e50 '

Your English is good, you can understand it well.

As we all see, in the classification I implemented, I defined a property, in. m nothing to do, how can it be so simple, yet not used on the tall on the operation is the mechanism!!

Classification. m modified as follows

#import "Person+address.h"

#import <objc/runtime.h>

@implementation Person (Address)

#pragma mark-address

static char Addresskey; //Similar to a broker, refer to

-(void) Setaddress: (NSString *) address {

objc_setassociatedobject (self, &addresskey, address, objc_association_copy_nonatomic );

}

-(nsstring*) address {

return objc_getassociatedobject (self, &addresskey);

}

@end

OK, now you can rest assured that the use, rest assured that the compilation. In this. m file is primarily a runtime mechanism. Remember to import header files: #import <objc/runtime.h>

There are two functions for objc_setassociatedobject and objc_getassociatedobject . What do you mean, it's recommended to look at the documentation.

Thank God read this blog, if there is inappropriate, hope guidance! qq:771722918

  

iOS adds attributes to a category

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.