NSDirectory与NSObject自动转换

本文探讨了在iOS开发中如何处理NSDirectory与NSObject之间的转换。重点关注`setValue:forUndefinedKey:`方法,该方法允许子类自定义处理未定义键的设置请求。默认实现会引发异常。此外,文章提供了一个示例链接,展示了不使用`setValuesForKeysWithDictionary`而是手动赋值的方式,但这种方式并不推荐。
摘要由CSDN通过智能技术生成
NSDirectory-->NSObject
[object setValuesForKeysWithDictionary:properties]
NSObject--> NSDirectory
NSDictionary *properties = [anObject dictionaryWithValuesForKeys:[anObject allKeys]];


注:
1、NSDirectory-->NSObject
可以封装一下(或者可以直接扩展到NSObject中):
// In your custom class 
+ (id)customClassWithProperties:(NSDictionary *)properties 
{
   return [[[self alloc] initWithProperties:properties] autorelease];
} 

- (id)initWithProperties:(NSDictionary *)properties 
{
   if (self = [self init]) 
     {
      [self setValuesForKeysWithDictionary:properties]; 
     } 
   return self;
 }

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值