gpt4 book ai didi

ios - NSMutableDictionary 中不区分大小写的搜索

转载 作者:可可西里 更新时间:2023-11-01 04:19:38 24 4
gpt4 key购买 nike

嗨,我有一个 NSMutableDictionary 包含小写和大写键。所以目前我不知道如何使用 objective-c 在字典中找到键而不考虑键。

最佳答案

救援类别。好的,所以这是一个旧帖子...

@interface NSDictionary (caseINsensitive)
-(id) objectForCaseInsensitiveKey:(id)aKey;
@end


@interface NSMutableDictionary (caseINsensitive)
-(void) setObject:(id) obj forCaseInsensitiveKey:(id)aKey ;
@end


@implementation NSDictionary (caseINsensitive)

-(id) objectForCaseInsensitiveKey:(id)aKey {
for (NSString *key in self.allKeys) {
if ([key compare:aKey options:NSCaseInsensitiveSearch] == NSOrderedSame) {
return [self objectForKey:key];
}
}
return nil;
}
@end


@implementation NSMutableDictionary (caseINsensitive)

-(void) setObject:(id) obj forCaseInsensitiveKey:(id)aKey {
for (NSString *key in self.allKeys) {
if ([key compare:aKey options:NSCaseInsensitiveSearch] == NSOrderedSame) {
[self setObject:obj forKey:key];
return;
}
}
[self setObject:obj forKey:aKey];
}

@end

享受。

关于ios - NSMutableDictionary 中不区分大小写的搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6135000/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com