wei*_*wei 3 copy properties objective-c retain nsmutabledictionary
首先我读了这篇文章
我想我应该在我的程序中使用"复制".问题是使用NSMutableDictionary复制它会终止.
*****由于未捕获异常'NSInternalInconsistencyException'而终止应用程序,原因:' - [__ 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,它执行正常.
Colud有人告诉我我的代码有什么问题吗?
如果我的代码与dealloc不正确,请告诉我.
谢谢appriciate.
是的,所以我修理了我的二传手:
-(void) setEvents:(NSMutableDictionary*) pEvents
{
    NSMutableDictionary* tNewDict = [pEvents mutableCopy];
    [gEvents removeAllObjects];
    [gEvents release];
    gEvents = tNewDict;
}
这项工作没有错误.
它对我很有帮助.
但我不能投票>"<〜
谢谢你巴伐利亚:)
小智 6
通常,应该使用可变属性来retain代替copy.当您声明属性时copy,合成的setter方法将发送-copy给分配给该属性的对象.在可变对象(例如NSMutableDictionary)的情况下,发送-copy给它们产生不可变副本,有效地创建不可变类型的对象(例如NSDictionary).
所以在:
tBtnXML.Events = [self SplitEvents:tNode];
合成的setter发送-copy给[self SplitEvents:tNode],从而创建该字典的不可变副本(即NSDictionary实例),并将其分配给gEvents.这是导致错误的原因:gEvents声明为NSMutableDictionary但指向了错误NSDictionary.
对于记录,可变类通常声明一个-mutableCopy确实生成可变副本的方法.但是,它不被声明的属性使用.如果您不想使用retain,则需要实现使用的自定义setter -mutableCopy.