2011-08-08 56 views
0

我有一个名为A的类;关于设置NSDictionary的键的问题

在A.H

@interface A : NSObject { 

    NSString *str; 
    NSNumber *num; 

} 

@property (nonatomic,retain) NSString *str; 
@property (nonatomic,retain) NSNumber *num; 

A是B.

的超类在B.h

@interface B : A { 

    NSString *BStr; 

} 

@property (nonatomic, retain) NSString *BStr; 

@end 

现在我需要设置B的对象作为一个的NSDictionary的关键。

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    B *key = [[B alloc] init]; 
    NSDictionary *dict = [NSDictionary dictionaryWithObject:@"object" forKey:key]; 
} 

然后B类应该实现NSCopying协议。

我想知道这段代码是否正确?我应该做一些关于A类财产的事吗?

在B.m

- (id) copyWithZone:(NSZone *)zone { 
    B *copy = [[[self class] allocWithZone:zone] init]; 
    copy.BStr = [[self.BStr copyWithZone:zone] autorelease]; 
    return copy; 
} 

谢谢!

回答

0

你必须STR和num复制copyWithZone太:

copy.str = [[self.str copyWithZone:zone] autorelease]; 
copy.num = [[self.num copyWithZone:zone] autorelease];