Objective c @保留或复制财产

Objective c @保留或复制财产,objective-c,copy,properties,nsmutabledictionary,retain,Objective C,Copy,Properties,Nsmutabledictionary,Retain,首先我读了这个 我想我应该在我的程序中使用“复制”。 问题是使用NSMutableDictionary copy时,它将终止 *****由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“-[\uu NSCFDictionary removeAllObjects]:将变异方法发送到不可变对象”** 我不知道“将方法变异发送到不可变对象”。 我没有将NSDictionary设置为NSMutabledictionary指针 这是我的密码 .

首先我读了这个

我想我应该在我的程序中使用“复制”。 问题是使用NSMutableDictionary copy时,它将终止

*****由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“-[\uu NSCFDictionary removeAllObjects]:将变异方法发送到不可变对象”**

我不知道“将方法变异发送到不可变对象”。 我没有将NSDictionary设置为NSMutabledictionary指针

这是我的密码



.h文件

@interface Button : NSObject {

@private
    NSString*               gID;                                 
    NSString*               gBackColor;                          
    NSString*               gIconImage;                          
    int                     gIndex;                              
    BOOL                    gEnable;                            
    BOOL                    gVisible;
    NSString*               gText;
    
    NSMutableDictionary*    gEvents;
    
    
    BOOL                    gUseCircle;                 
}

@property (nonatomic,copy) NSString                 *ID;
@property (nonatomic,copy) NSString                 *BackColor;
@property (nonatomic,copy) NSString                 *IconImage;
@property int Index;
@property BOOL Enable;
@property BOOL Visible;
@property (nonatomic,copy) NSString                 *Text;
@property (nonatomic,getter=getEvents,retain) NSMutableDictionary       *Events;
@property BOOL UseCircle;

@end


.m文件

@implementation Button
@synthesize ID = gID;
@synthesize BackColor = gBackColor;
@synthesize IconImage = gIconImage;
@synthesize Index = gIndex;
@synthesize Enable = gEnable;
@synthesize Visible = gVisible;
@synthesize Text = gText;
@synthesize Events = gEvents;
@synthesize UseCircle = gUseCircle;

-(NSMutableDictionary*) getEvents
{
    if (!gEvents) 
    {
        gEvents = [[NSMutableDictionary alloc] initWithCapacity:20];
    }
    return gEvents;
}

- (id) init
{
    self = [super init];
    if (self != nil) 
    {
        gID = @"";
        gBackColor = @"";
        gIconImage = @"";
        gIndex = 0;
        gText = @"";
        
        gUseCircle = NO;
    }
    return self;
}

- (void) dealloc
{
    [gID release];
    [gBackColor release];
    [gIconImage release];
    [gText release];
    
    [gEvents removeAllObjects];
    [gEvents release];
    gEvents = nil;
    
    [super dealloc];
}


实施

tBtnXML.Events = [self SplitEvents:tNode];


SplitEvents功能:

-(NSMutableDictionary*) SplitEvents:(NSDictionary*)pEvents
{
    NSMutableDictionary *tEvents = [[NSMutableDictionary alloc] initWithCapacity:5];
    // code blabla
    //.
    //.
    //.
    [tEvents setObject:tEvent forKey:[NSNumber numberWithInt:tEventName]];
    [tEvent release];
            
            

            return [tEvents autorelease];
}

但我将NSMutableDictionary*gEvents属性从copy更改为retain,它将正常执行

有人告诉我我的代码出了什么问题吗

如果我的代码与dealloc不正确,请告诉我

谢谢你的赞赏。





是的,所以我修好了我的setter:

-(void) setEvents:(NSMutableDictionary*) pEvents
{
    NSMutableDictionary* tNewDict = [pEvents mutableCopy];
    [gEvents removeAllObjects];
    [gEvents release];
    gEvents = tNewDict;
}

这项工作没有错误

这对我帮助很大


但是我不能投票赞成>“一般来说,可变属性应该是
retain
而不是
copy
。当您将属性声明为
copy
时,综合setter方法将
-copy
发送到分配给该属性的对象。对于可变对象(例如,
NSMutableDictionary
),将
-copy
发送给它们会生成一个不可变的副本,有效地创建一个不可变类型的对象(例如,
NSDictionary

因此:

tBtnXML.Events = [self SplitEvents:tNode];
综合setter将
-copy
发送到
[self-SplitEvents:tNode]
,从而创建该字典的不可变副本(即
NSDictionary
实例),并将其分配给
gEvents
。这是导致错误的原因:
gEvents
声明为
NSMutableDictionary
,但指向
NSDictionary


作为记录,可变类通常声明一个生成可变副本的
-mutableCopy
方法。但声明的属性不使用该方法。如果不想使用
保留
,则需要实现一个自定义setter,使用
-mutableCopy

@wei
NSMutableDictionary
实现
-mutableCopy
-事实上,它符合
NSMutableCopy
协议。问题是,在合成Objective-C声明的属性时,setter方法不使用
-mutableCopy
。感谢您的回复:)是否奇怪
NSMutableDictionary
copy返回一个不可变的对象?我想我理解我的错误。好的~~~我明白了~>”