*
* copyWithZone ÊÇÔÚ [NSMutableDictionary setObject:@"test" forKey:KeyObject¶ÔÏó] ·½·¨µ÷ÓõÄʱºòµ÷ÓõÄ~
* ´óÒâ¾ÍÊǽ«×÷Ϊ×Öµä¼üµÄ¶ÔÏóÉ±´Ò»·Ý·ÅÔÚ×Öµä¶ÔÏóÀïÃæ¡£
* Èç¹û KeyObject ×÷Ϊ×ÖµäµÄ¼ü¶ÔÏóÀ´Ê¹Óã¬Ê¹ÓÃÍê±ÏºóÐ轫¼ü¶ÔÏó release µô£¬ÒòΪ×ֵ䲢²»»áʹÓÃÕâ¸ö¶ÔÏó£¬
* ¶øÊÇ»á¿Ë¡³öÒ»¸ö°üº¬ÏàͬÊý¾ÝµÄ¶ÔÏóÀ´~
*
* ÒÔÉÏÒ²ÊÇ setObject forKey ǰºó£¬ÎªÊ²Ã´¼ü¶ÔÏóµÄ retainCount ÒÀȻΪ 1 µÄÔÒò~
* (×¢£ºsetObject forKey ǰºó£¬ Öµ¶ÔÏóµÄ retainCount »á¼Ó 1~)
*/
-(id) copyWithZone:(NSZone*)zone {
NSLog(@"KeyObject.copyWithZone() ·½·¨±»µ÷ÓÃ~");
// ÕýÈ·µÄд·¨~
return [[KeyObject alloc] initWithX:_x y:_y];
// ´íÎóµÄд·¨~
// return [KeyObject kObjectWithX:_x y:_y];
}
-(NSString*) description {
return [NSString stringWithFormat:@"_x=%d, _y=%d", _x, _y];
}
-(NSUInteger) hash {
// NSLog(@"KeyObject.hash() ·½·¨±»µ÷ÓÃ~");
return 0;
}
-(BOOL) isEqual:(id)object {
// NSLog(@"KeyObject.isEqual() ·½·¨±»µ÷ÓÃ~");
if(![object isKindOfClass:[KeyObject class]]) {
// NSLog(@"Object passed in isn't a KeyObject instance!");
return NO;
}
KeyObject* tmp = (KeyObject*)object;
if(tmp.x == _x && tmp.y == _y) {
// NSLog(@"## Equals ¡ª¡ª tmp.x=%d, tmp.y=%d, _x=%d, _y=%d", tmp.x, tmp.y, _x, _y);
return YES;
} else {
// NSLog(@"## Not Equals ¡ª¡ª tmp.x=%d, tmp.y=%d, _x=%d, _y=%d", tmp.x, tmp.y, _x, _y);
return NO;
}
}
-(void) dealloc {
NSLog(@"%@%@", [self description], @" dealloc!");
[super dealloc];
}
@end
//
// KeyObject.m
// DictionaryKeyObject
//
// Created by BruceYang on 12-7-29.
// Copyright (c) 2012Äê EricGameStudio. All rights reserved.
//
#import "KeyObject.h"
@implementation KeyObject
@synthesize x = _x;
@synthesize y = _y;
+(id) kObjectWithX:(int)x y:(int)y {
return [[[self alloc] initWithX:x y:y] autorelease];
}
-(id) initWithX:(int)x y:(int)y {
if((self = [super init])){
_x = x;
_y = y;
}
return self;
}
/**
* ×¢Ò⣺
* ²»ÄÜ autorelease£¡setObject:forKey: µÄʱºò£¬key ¶ÔÏóµÄ retainCount ²¢²»»á¼Ó 1~
* autorelease µÄ»°µ¼ÖÂÒ»ÍÑÀëËùÔÚµÄÓò£¬¼ü¶ÔÏó¾ÍʧЧ£¬Òý·¢ EXEC_BAD_ACCESS ´íÎó~
* Óà Instruments ×ö allocation ²âÊÔ£¬¿ÉÒÔ·¢ÏÖ¼ü¶ÔÏóÒ»Ö±ÊÇ´¦ÓÚ´æ»î״̬µÄ£¬Õâ²ÅÊÇÕý³£µÄÇé¿ö~
*
* copyWithZone ÊÇÔÚ [NSMutableDictionary setObject:@"test" forKey:KeyObject¶ÔÏó] ·½·¨µ÷ÓõÄʱºòµ÷ÓõÄ~
* ´óÒâ¾ÍÊǽ«×÷Ϊ×Öµä¼üµÄ¶ÔÏóÉ±´Ò»·Ý·ÅÔÚ×Öµä¶ÔÏóÀïÃæ¡£
* Èç¹û KeyObject ×÷Ϊ×ÖµäµÄ¼ü¶ÔÏóÀ´Ê¹Óã¬Ê¹ÓÃÍê±ÏºóÐ轫¼ü¶ÔÏó release µô£¬ÒòΪ×ֵ䲢²»»áʹÓÃÕâ¸ö¶ÔÏó£¬
* ¶øÊÇ»á¿Ë¡³öÒ»¸ö°üº¬ÏàͬÊý¾ÝµÄ¶ÔÏóÀ´~
*
* ÒÔÉÏÒ²ÊÇ setObject forKey ǰºó£¬ÎªÊ²Ã´¼ü¶ÔÏóµÄ retainCount ÒÀȻΪ 1 µÄÔÒò~
* (×¢£ºsetObject forKey ǰºó£¬ Öµ¶ÔÏóµÄ retainCount »á¼Ó 1~)
*/
-(id) copyWithZone:(NSZone*)zone {
NSLog(@"KeyObject.copyWithZone() ·½·¨±»µ÷ÓÃ~");
// ÕýÈ·µÄд·¨~
return [[KeyObject alloc] initWithX:_x y:_y];
// ´íÎóµÄд·¨~
// return [KeyObject kObjectWithX:_x y:_y];
}
-(NSString*) description {
return [NSString stringWithFormat:@"_x=%d, _y=%d", _x, _y];
}
-(NSUInteger) hash {
// NSLog(@"KeyObject.hash() ·½·¨±»µ÷ÓÃ~");
return 0;
}
-(BOOL) isEqual:(id)object {
// NSLog(@"KeyObject.isEqual() ·½·¨±»µ÷ÓÃ~");
if(![object isKindOfClass:[KeyObject class]]) {
// NSLog(@"Object passed in isn't a KeyObject instance!");
return NO;
}
KeyObject* tmp = (KeyObject*)object;
if(tmp.x == _x && tmp.y == _y) {
// NSLog(@"## Equals ¡ª¡ª tmp.x=%d, tmp.y=%d, _x=%d, _y=%d", tmp.x, tmp.y, _x, _y);
return YES;
} else {
// NSLog(@"## Not Equals ¡ª¡ª tmp.x=%d, tmp.y=%d, _x=%d, _y=%d", tmp.x, tmp.y, _x, _y);
return NO;
}
}
-(void) dealloc {
NSLog(@"%@%@", [self description], @" dealloc!");
[super dealloc];
}