首先我阅读了这篇文章
我认为我应该在我的程序中使用"copy"。 问题是当我使用NSMutableDictionary时,它会终止。
***** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[__NSCFDictionary removeAllObjects]: mutating method sent to immutable object'**
我不知道什么是"mutating method sent to immutable object"。 我没有将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];
}
And implement
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;
}
这个工作没有出现错误。
它帮助了我很多。
但是我无法投票,>"<~
所以感谢Bavarious :)
NSMutableDictionary
实现了-mutableCopy
— 实际上,它符合NSMutableCopying
协议。问题在于,在 Objective-C 声明属性时综合 setter 方法 不使用-mutableCopy
。 - user557219NSMutableDictionary
的复制返回一个不可变对象,这是很奇怪的吗? 我想我明白了我的错误。 - wei