开发者

Custom single KeyValuePair class vs NSMutableDictionary

I came into a situation where I had to write a loop with a good amount if iterations and in this loop I had a NSData object that I had to associate with a key. This lead me to search for a simple objective-c _KeyValuePair_ class but coulnt not find one so I wrote my own. Now I'm curious to see if there is any benefit over just using an NSMutableDictinoary holding just 1 key and value. After trying both throughout my project I can't tell much difference on the App UI side or with Instruments Time Profiler.

So my questions are:

  • Could a single kvpair class be more efficient than a NSMutableDictionary
  • Doe开发者_高级运维s a NSMutableDict allocate any larger amount of space by default then this does
  • Is there actually a standard single key value pair class that I just missed

Some code:

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

Edit to fix the initial explanation. Seems I got sidetracked mid-sentance and never came back to finish it.


If you really want to get speed you are doing a lot of unnecessary retain releases that probably aren't necessary every time you set your key/values. If you use a struct and some basic c code you can achieve something a little quicker but you sacrifice the simple and consistent memory management you get from doing it the objective c way.

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];
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜