0
#import <Foundation/Foundation.h>

@interface Location : NSObject
{
    int x_;
    int y_;
}
@property(assign) int x;
@property(assign) int y;
@end


#import "Location.h"


@implementation Location

@synthesize x = x_;
@synthesize y = y_;
@end


-(id) calcKey:(int)x theY:(int)y{
    Location* loc = [[Location alloc]init];
    loc.x = x;
    loc.y = y;
    return loc;
} 

for(id innerObj in arr){
   NSMutableDictionary* dic = [NSMutableDictionary dictionary];
   for(id innerObj in arr){
      MyStorage* storage = (MyStorage*)innerObj;
      id key = [self calcKey:storage.x theY:storage.y];
      [dic setObject:bri forKey:key];
   }
}

嗨,我正在尝试将存储添加到 NSMutableDictionary,我的目的是使用此字典 dic 来加速进一步搜索位置 (x,y) 具有存储的位置。但它总是失败......带有以下警告,任何人都可以帮助我 ?非常感谢!:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Location copyWithZone:]: unrecognized selector sent to instance 0x6b4b160'
4

1 回答 1

0

由于您使用自定义类的实例Location作为键,并且在字典中使用时会复制键,因此您必须通过实现来实现对复制的支持copyWithZone:

方法名称的“区域”部分是指一些不再使用的旧内存管理方案,因此您可以忽略它。

正如评论中所要求的,您的copyWithZone:方法应该简单地创建一个新对象,设置实例变量并返回新实例。您可能还想覆盖isEqual:and hash。更多在这里

于 2012-06-24T08:14:36.337 回答