KVC 的 容错方法

来源:互联网 发布:知乎 感情 精华贴 编辑:程序博客网 时间:2024/06/01 18:08

#pragma mark KVC容错方法

-(void)setValue:(id)value forUndefinedKey:(NSString *)key{


    NSLog(@"fwefwefwefwefwef23222\n\n\n\n\n");

    if ([key isEqualToString:@"IsOnlyXuanZuo"]) {


        NSString *str = [NSString stringWithFormat:@"%@", value ];

        self.IsOnlyXuanZuo = value;

    }

    if ([key isEqualToString:@"IsToBeAboutTo"]) {

        NSString *str = [NSString stringWithFormat:@"%@", value ];

        self.IsToBeAboutTo = value;

    }

}



/**********************        分隔符         **********************/

//

//  CinemaList.h

//  UI11_作业01展示影院位置

//

//  Created by Rickie on 15/11/27.

//  Copyright (c) 2015 Rickie. All rights reserved.

//


#import <Foundation/Foundation.h>


@interface CinemaList : NSObject


/**

 *  电影院的地址

 */

@property (nonatomiccopyNSString *address;


/**

 *  电影院名称

 */

@property (nonatomiccopyNSString *cinemaName;


/**

 *  城市名

 */

@property (nonatomiccopyNSString *cityName;


/**

 *  编号

 */

@property (nonatomiccopyNSString *ID;


/**

 *  纬度

 */

@property (nonatomiccopyNSString *latitude;


/**

 *  经度

 */

@property (nonatomiccopyNSString *longitude;


/**

 *  电话号码

 */

@property (nonatomiccopyNSString *telephone;



/**

 *  交通线路

 */

@property (nonatomiccopyNSString *trafficRoutes;


//初始化方法和便利构造器

-(id)initWithDictionary:(NSDictionary *)dictionary;


+(CinemaList *)cinemaListWithDictionary:(NSDictionary *)dictionary;


@end


/**********************        分隔符         **********************/

//

//  CinemaList.m

//  UI11_作业01展示影院位置

//

//  Created by Rickie on 15/11/27.

//  Copyright (c) 2015 Rickie. All rights reserved.

//


#import "CinemaList.h"


@implementation CinemaList


-(void)dealloc

{

    [_address release];

    [_cinemaName release];

    [_cityName release];

    [_ID release];

    [_latitude release];

    [_longitude release];

    [_telephone release];

    [_trafficRoutes release];

    [super dealloc];

}



#pragma mark 初始化方法

-(id)initWithDictionary:(NSDictionary *)dictionary

{

    self = [super init];

    if (self) {

        //KVC 赋值方法 传值,将字典里的数据传入到类里的属性里面

        [self setValuesForKeysWithDictionary:dictionary];

    }

    return self;

}


#pragma mark 遍历构造器

+(CinemaList *)cinemaListWithDictionary:(NSDictionary *)dictionary

{

    CinemaList *cinemaList = [[CinemaList allocinitWithDictionary:dictionary];

    return [cinemaList autorelease];

}


/**********************        当给的资料中有属性、key值为id时,使用容错处理         **********************/


#pragma mark KVC容错处理

-(void)setValue:(id)value forUndefinedKey:(NSString *)key

{

    if ([key isEqualToString:@"id"]) {

        self.ID = value;

    }

}



@end


/**********************        分隔符         **********************/


0 0