我应该如何构造一个具有多个键的 NSDictionary?

发布于 2024-08-08 04:56:09 字数 251 浏览 5 评论 0原文

我有一种情况,我想将一对对象映射到信息字典。我想避免创建 NSDictionaries of NSDictionaries of NSDictionaries 的 NSDictionary,因为这很令人困惑且难以维护。

例如,如果我有两个类,Foo 和 Bar,我想要 {Foo, Bar} -> {NSDictionary}

除了根据相关的两种类型创建自定义类(仅用作字典键)之外,还有其他选择吗?也许是类似于 STL 的配对类型但我不知道的东西?

I have a situation where I want to map a pair of objects to a dictionary of information. I'd like to avoid creating an NSDictionary of NSDictionaries of NSDictionaries since that's simply confusing and difficult to maintain.

For instance if I have two classes, Foo and Bar, I want {Foo, Bar} -> {NSDictionary}

Are there other options than just making a custom class (just to be used as the dictionary key) based on the two types in question? Maybe something akin to STL's pair type that I just don't know about?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

秋叶绚丽 2024-08-15 04:56:10

就像你说的,你可以创建一个 Pair 类:

//Pair.h
@interface Pair : NSOpject <NSCopying> {
  id<NSCopying> left;
  id<NSCopying> right;
}
@property (nonatomic, readonly) id<NSCopying> left;
@property (nonatomic, readonly) id<NSCopying> right;
+ (id) pairWithLeft:(id<NSCopying>)l right:(id<NSCopying>)r;
- (id) initWithLeft:(id<NSCopying>)l right:(id<NSCopying>)r;
@end

//Pair.m
#import "Pair.h"
@implementation Pair
@synthesize left, right;

+ (id) pairWithLeft:(id<NSCopying>)l right:(id<NSCopying>)r {
 return [[[[self class] alloc] initWithLeft:l right:r] autorelease];
}

- (id) initWithLeft:(id<NSCopying>)l right:(id<NSCopying>)r {
  if (self = [super init]) {
    left = [l copy];
    right = [r copy];
  }
  return self;
}

- (void) finalize {
  [left release], left = nil;
  [right release], right = nil;
  [super finalize];
}

- (void) dealloc {
  [left release], left = nil;
  [right release], right = nil;
  [super dealloc];
}

- (id) copyWithZone:(NSZone *)zone {
  Pair * copy = [[[self class] alloc] initWithLeft:[self left] right:[self right]];
  return copy;
}

- (BOOL) isEqual:(id)other {
  if ([other isKindOfClass:[Pair class]] == NO) { return NO; }
  return ([[self left] isEqual:[other left]] && [[self right] isEqual:[other right]]);
}

- (NSUInteger) hash {
  //perhaps not "hashish" enough, but probably good enough
  return [[self left] hash] + [[self right] hash];
}

@end

编辑:

关于创建可以作为键的对象的一些注意事项:

  1. 它们必须符合 NSCopying 协议,因为我们不希望键改变从我们下面。
  2. 由于对本身被复制,我确保对中的对象也被复制。
  3. 键必须实现 isEqual:。我实现了 hash 方法以达到良好的效果,但这可能不是必需的。

Like you said, you can create a Pair class:

//Pair.h
@interface Pair : NSOpject <NSCopying> {
  id<NSCopying> left;
  id<NSCopying> right;
}
@property (nonatomic, readonly) id<NSCopying> left;
@property (nonatomic, readonly) id<NSCopying> right;
+ (id) pairWithLeft:(id<NSCopying>)l right:(id<NSCopying>)r;
- (id) initWithLeft:(id<NSCopying>)l right:(id<NSCopying>)r;
@end

//Pair.m
#import "Pair.h"
@implementation Pair
@synthesize left, right;

+ (id) pairWithLeft:(id<NSCopying>)l right:(id<NSCopying>)r {
 return [[[[self class] alloc] initWithLeft:l right:r] autorelease];
}

- (id) initWithLeft:(id<NSCopying>)l right:(id<NSCopying>)r {
  if (self = [super init]) {
    left = [l copy];
    right = [r copy];
  }
  return self;
}

- (void) finalize {
  [left release], left = nil;
  [right release], right = nil;
  [super finalize];
}

- (void) dealloc {
  [left release], left = nil;
  [right release], right = nil;
  [super dealloc];
}

- (id) copyWithZone:(NSZone *)zone {
  Pair * copy = [[[self class] alloc] initWithLeft:[self left] right:[self right]];
  return copy;
}

- (BOOL) isEqual:(id)other {
  if ([other isKindOfClass:[Pair class]] == NO) { return NO; }
  return ([[self left] isEqual:[other left]] && [[self right] isEqual:[other right]]);
}

- (NSUInteger) hash {
  //perhaps not "hashish" enough, but probably good enough
  return [[self left] hash] + [[self right] hash];
}

@end

Edit:

Some notes on creating objects that can be keys:

  1. They have to conform to the NSCopying protocol, since we don't want the key to change out from underneath us.
  2. Since the Pair itself is copied, I make sure the objects in the pair are also copied.
  3. Keys have to implement isEqual:. I implement the hash method for good measure, but it's probably not necessary.
生来就爱笑 2024-08-15 04:56:10

您可以尝试 NSMapTable,然后您的密钥几乎可以是任何东西,如果您愿意的话,也许可以是指向结构的指针。

(以下评论的附录:NSMapTable 曾经不在 iOS 上,但从 iOS 6 开始可用。)

You could try NSMapTable, then your key can be pretty much anything, perhaps a pointer to a structure if you want.

(Addendum to the comments below: NSMapTable was once not on iOS, but is available as of iOS 6.)

遗失的美好 2024-08-15 04:56:10

效果的东西

使用[NSString stringWithFormat:@"%@ %@", [obj1 key_as_string], [oj2 key_as_string]]

对你没有好处,其中 key_as_string 是你的对象类的一部分?即从字符串中创建一个密钥。

Is using something to the effect of

[NSString stringWithFormat:@"%@ %@", [obj1 key_as_string], [oj2 key_as_string]]

no good for you, where key_as_string is part of your object class? i.e. make a key from a string.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文