ericarlene 2016-04-25 08:47 采纳率: 0%
浏览 2043

iOS中在类别中动态新增了的可变字典属性变成了不可变字典

想在category中新增个属性来用,创建的可变字典属性,也做了相关关联,结果用不了setobject方法,打印出来一看发现变成了不可变字典类型,希望有大神帮我看看哪里出问题了:

static void *myAllPropertiesDicKey = &myAllPropertiesDicKey;

@implementation FatherClass (Category)
//add setter and getter method in runtime
-(void)setMyAllPropertiesDic:(NSMutableDictionary *)myAllPropertiesDic{
objc_setAssociatedObject(self, & myAllPropertiesDicKey, myAllPropertiesDic, OBJC_ASSOCIATION_COPY);
}

-(NSMutableDictionary )myAllPropertiesDic{
return objc_getAssociatedObject(self, &myAllPropertiesDicKey);
}
//
-(instancetype)initByAllPropertiesInMyDictionary{
if (self = [super init]) {
//declare a variable to save the number of properties
unsigned int count;
//get all the properties
objc_property_t
properties = class_copyPropertyList([self class], &count);
//init the mutabledictionary used to save all properties
self.myAllPropertiesDic = [[NSMutableDictionary alloc]initWithCapacity:count];
//deal with each property
for (int i=0; i<count; i++) {
objc_property_t property = properties[i];
const char* char_f = property_getName(property);
NSString* propertyName = [NSString stringWithUTF8String:char_f];
id propertyValue = [self valueForKey:propertyName];
if (![propertyName isEqualToString:@"myAllPropertiesDic"]) {
if ([self.myAllPropertiesDic isKindOfClass:[NSMutableDictionary class]]) {
NSLog(@"可变");
}else if ([self.myAllPropertiesDic isKindOfClass:[NSDictionary class]]){
NSLog(@"不可变");
}else{
NSLog(@"p都不是");
}
[self.myAllPropertiesDic setObject:propertyValue?propertyValue:[NSNull null] forKey:propertyName];
[self addObserver:self forKeyPath:propertyName options:NSKeyValueObservingOptionNew context:nil];
}
}
free(properties);

}

return self;

}
打印出来是:2016-04-25 16:31:36.551 test2[1375:54201] 不可变
第一次发问题,谢谢大家了

  • 写回答

1条回答 默认 最新

  • W_C__L 博客专家认证 2016-04-29 02:25
    关注

    你用runtime创建字典是用的是copy修饰符导致你的字典是不可变,应该用strong修饰符,改了就好用了

    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP