iphone 常用函数汇总

本文介绍了如何通过Objective-C语言,利用setValuesForKeysWithDictionary函数,将字典中的键值对自动赋值给对象的各个成员变量。详细解释了实现过程,包括定义类、初始化对象以及遍历字典进行赋值。

1:将字典中的键值对赋值给对象的各个成员变量。关键字:

setValuesForKeysWithDictionary

   1.1先定义一个类,该类为Quotation    声明如下:

 

          @interface Quotation : NSObject {


}

@property (nonatomic, retain) NSString *character;
@property (nonatomic, assign) NSInteger act;
@property (nonatomic, assign) NSInteger scene;
@property (nonatomic, retain) NSString *quotation;

@end 

 

 @implementation Quotation


@synthesize character, act, scene, quotation;

- (void)dealloc {
    [character release];
    [quotation release];
    [super dealloc];
}

@end

 

 1.2 然后自动从字典中将值取到对象中。使用了“setValuesForKeysWithDictionary”这么一个函数,代码如下:

   

      NSArray *quotationDictionaries = [playDictionary objectForKey:@"quotations"];

        NSMutableArray *quotations = [NSMutableArray arrayWithCapacity:[quotationDictionaries count]];
        
        for (NSDictionary *quotationDictionary in quotationDictionaries) {
         
            Quotation *quotation = [[Quotation alloc] init];
            [quotation setValuesForKeysWithDictionary:quotationDictionary];
            
            [quotations addObject:quotation];
            [quotation release];

        }

 

          

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值