Advantages of KVC in defining model classes

Source: Internet
Author: User

@ Our application uses the MVC Architecture. for data processing classes, we define a model class separately and assign initialization values to the properties to be displayed, the general method is to assign values one by defining the corresponding attributes. now I want to introduce how to assign values through the KVC and key-value methods.

@ Let's look at a piece of code:

# Import <Foundation/Foundation. h> @ interface mdcallshopobjectmodel: nsobject // defines the attributes of the content to be displayed for all shop information classes @ property (nonatomic, copy) nsstring * Name; // store name @ property (nonatomic, copy) nsstring * businessid; // attribute shop ID @ property (nonatomic, copy) nsstring * address; // shop address @ property (nonatomic, copy) nsstring * telephone; // shop phone @ property (nonatomic, copy) nsstring * business_id; // KVC shop ID/*** customize the initialization method of a business point information class ** @ Param DIC the data of the Group Buying store parsed by Param DIC is the dictionary ** @ return shop object */- (ID) initwithdictionary: (nsdictionary *) DIC;/*** KVC value assignment method ** @ Param kvcdic the data of the Group Buying store parsed is a dictionary ** @ return shop object */-(ID) initwithkvcdictionary :( nsdictionary *) kvcdic; @ end

#import "MDCAllShopObjectModel.h"@implementation MDCAllShopObjectModel- (id)initWithDictionary:(NSDictionary *)dic{    if (self = [super init]) {                self.name        = dic[@"name"];        self.businessId  = [dic[@"business_id"] stringValue];        self.address     = dic[@"address"];        self.telephone   = dic[@"telehpone"];        }        return self;}- (id)initWithKVCDictionary:(NSDictionary *)KVCDic{     if (self = [super init]) {        [self setValuesForKeysWithDictionary:KVCDic];            }    return self;}- (void)setValue:(id)value forUndefinedKey:(NSString *)key{    NSLog(@"key = %@",key);}
@ First look at the. h file. I have defined two store ID attributes to demonstrate the difference. first declare that the data ID returned by the public comment interface is "business_id ".

1. when assigning values using the attribute method, the obtained attribute name can not be the same as the attribute name in the request data. When assigning values using the KVC method, it must be identical. Remember, the case cannot be incorrect.

2. attribute assignment: the number of defined attributes is very free. You need to define the number of attributes that are not limited by the number of attributes returned by the request data interface. The KVC method is used to assign values, the setvaluesforkeyswithdictionary method traverses all attributes in the requested dictionary, and all keys are assigned values one by one. In this case, you need to define all attributes in the model to receive them.

3. it is convenient to assign values using the KVC method. After all, you just need to use the-(void) setvalue :( ID) value forundefinedkey :( nsstring *) Key Method to Solve the Problem in step 2, when you implement this method, you can assign values in the KVC mode and define only the attributes you need, because when setvaluesforkeyswithdictionary cannot find the corresponding key, the program will call the forundefinedkey method to store the value corresponding to the key into the value, so that the program will not crash.

4. When the requested data contains system keywords such as "ID" and "Description", the forundefinedkey method can also be used. For example:

-(Void) setvalue :( ID) value forundefinedkey :( nsstring *) Key {// when the public comment interface property store ID is not "bussine_id" but "ID, "ID" is not compatible with key-value encoding if ([Key isequaltostring: @ "ID"]) {self. bussine_id = value ;}}
5. In the crash issue in the third point, I will explain it with the actual test results:





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.