【问题标题】:Custom single KeyValuePair class vs NSMutableDictionary自定义单个 KeyValuePair 类与 NSMutableDictionary
【发布时间】:2011-09-27 09:03:53
【问题描述】:

我遇到了一种情况,我必须编写一个具有大量 if 迭代的循环,并且在这个循环中我有一个 NSData 对象,我必须将它与一个键关联。这导致我搜索一个简单的objective-c _KeyValuePair_ 类,但找不到一个,所以我自己写了。现在我很想知道仅使用 NSMutableDictinoary 仅持有 1 个键和值是否有任何好处。在整个项目中都尝试了这两种方法后,我在 App UI 端或 Instruments Time Profiler 方面没有太大区别。

所以我的问题是:

  • 单个 kvpair 类是否比NSMutableDictionary 更有效?
  • 默认情况下NSMutableDict 是否会分配更大的空间,然后这样做
  • 真的有我刚刚错过的标准单键值对类

一些代码:

for (int i = 0, count = [photoUrls count]; i < count; ++i) {
    // Example usage of the kvp class
    NSMutableDictionary *imageRequest = [[NSMutableDictionary alloc] init];
    JHKeyValuePair *kvPair = [[JHKeyValuePair alloc] initWithKey:@"DAILY" andValue:[NSNumber numberWithInt:i];
    [imageRequest setObject:self forKey:@"delegate"];
    [imageRequest setObject:kvPair forKey:@"userInfo"];
    [kvPair release];
    [imageRequest setObject:[dailySpecialData objectForKey:@"IMAGE_URL"] forKey:@"url"];
    [imageDownloader addDownloadRequestToQueue:imageRequest];
    [imageRequest release];
}

JHKeyValuePair.h

@interface JHKeyValuePair : NSObject {
    id key;
    id value;
}

@property (nonatomic, retain) id key;
@property (nonatomic, retain) id value;

- (id)initWithKey:(id)aKey andValue:(id)aValue;

@end

JHKeyValuePair.m

#import "JHKeyValuePair.h"

@implementation JHKeyValuePair

@synthesize key;
@synthesize value;

- (id)initWithKey:(id)aKey andValue:(id)aValue {
    if ((self = [super init])) {
        key   = [aKey retain];
        value = [aValue retain];
    }
    return self;
}

- (void)dealloc {
    [key release], key = nil;
    [value release], value = nil;
    [super dealloc];
}

- (id)copyWithZone:(NSZone *)zone {
    JHKeyValuePair *copy = [[JHKeyValuePair allocWithZone:zone] init];
    [copy setKey:self.key];
    [copy setValue:self.value];
    return copy;
}

- (BOOL)isEqual:(id)anObject {
    BOOL ret;
    if (self == anObject) {
        ret = YES;
    } else if (![anObject isKindOfClass:[JHKeyValuePair class]]) {
        ret = NO;
    } else {
        ret = [key isEqual:((JHKeyValuePair *)anObject).key] && [value isEqual:((JHKeyValuePair *)anObject).value];
    }
    return ret;
}

@end

编辑以修正最初的解释。似乎我在句子中跑题了,再也没有回来完成它。

【问题讨论】:

    标签: objective-c performance nsmutabledictionary key-value nskeyvalue


    【解决方案1】:

    如果您真的想提高速度,那么您可能会在每次设置键/值时执行很多不必要的保留版本。如果您使用结构体和一些基本的 c 代码,您可以更快地完成一些事情,但您会牺牲从客观 c 方式中获得的简单且一致的内存管理。

    typedef struct {
        id key;
        id value;
    } Pair;
    
    BOOL isEqual(Pair a, Pair b); //...
    
    // You will need to clean up after yourself though:
    void PairRelease(Pair p) {
        [p.key release];
        [p.value release];
    }
    

    【讨论】:

    • 这是一个有趣的想法,并且可能会比在每次循环运行中使用 NSMutableDictionay 更快。我试试看,谢谢!似乎也与CF...Ref 方法的工作方式非常相似。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-13
    • 2013-10-16
    • 1970-01-01
    • 2022-01-22
    • 1970-01-01
    相关资源
    最近更新 更多