簡體   English   中英

NSDictionary:將NSString鍵轉換為小寫以在其上搜索字符串

[英]NSDictionary: convert NSString keys to lowercase to search a string on them

我正在開發一個iPhone應用程序。

我使用NSDictionary將城市的名稱存儲為關鍵,將人口存儲為值。 我想用小寫搜索鍵。

我用過這個:

NSDictionary *dict;

[dict objectForKey:[[city stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] lowercaseString]];

但是,它不起作用。

我知道,我可以做一個for,將鍵轉換為小寫並與城市進行比較。

還有其他辦法嗎? 也許,使用NSDictionary方法。

UPDATE
NSDictionary從屬性列表加載。

謝謝。

我在NSDictionary類別中使用此方法。

@implementation NSDictionary (MyCategory)
- (NSDictionary *)dictionaryWithLowercaseKeys {
    NSMutableDictionary *result = [NSMutableDictionary dictionaryWithCapacity:0];
    NSString *key;

    for (key in self) {
        [result setObject:[self objectForKey:key] forKey:[key lowercaseString]];
    }

    return result;
}
@end

雖然我仍然不清楚你想要什么,但這個循環搜索鍵不敏感。 獲得該密鑰的價值是微不足道的。

for key in dict
{
    if ([key caseInsensitiveCompare: @"Whatever"] == NSOrderedSame)
        NSLog(@"They are equal.");
}

以上答案僅適用於沒有任何嵌套的平面字典。 我創建了一個類別,它創建了一個現有NSDictionary的副本,每個鍵都轉換為小寫。

標題NSDictionary + LowercaseKeys.h

#import <Foundation/Foundation.h>

@interface NSDictionary (LowercaseKeys)

/*
 Recursive algorithm to find all nested dictionary keys and create an NSMutableDictionary copy with all keys converted to lowercase.
 Returns an NSMutableDictionary with all keys and nested keys converted to lowercase.
 */
+ (NSMutableDictionary *)dictionaryWithLowercaseKeysFromDictionary:(NSDictionary *)dictionary;

/*
 Convienience method to create a new lowercase dictionary object an existing NSDictionary instance
 Returns an NSMutableDictionary with all keys and nested keys converted to lowercase.
 */
- (NSMutableDictionary *)dictionaryWithLowercaseKeys;

@end

實現NSDictionary + LowercaseKeys.m

#import "NSDictionary+LowercaseKeys.h"

@implementation NSDictionary (LowercaseKeys)

/*
 Recursive algorithm to find all nested dictionary keys and create an NSMutableDictionary copy with all keys converted to lowercase
 Returns an NSMutableDictionary with all keys and nested keys converted to lowercase.
 */
+ (NSMutableDictionary *)dictionaryWithLowercaseKeysFromDictionary:(NSDictionary *)dictionary
{
    NSMutableDictionary *resultDict = [NSMutableDictionary dictionaryWithCapacity:[dictionary count]];
    [dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *key, id obj, BOOL *stop) {
        // There are 3 types of objects to consider, NSDictionary, NSArray and everything else
        id resultObj;
        if ([obj isKindOfClass:NSDictionary.class])
        {
            // Recursively dig deeper into this nested dictionary
            resultObj = [NSMutableDictionary dictionaryWithLowercaseKeysFromDictionary:obj];
        }
        else if ([obj isKindOfClass:NSArray.class])
        {
            /*
             Iterate over this nested NSArray. Recursively convert any NSDictionary objects to the lowercase version.
             If the array contains another array then continue to recursively dig deeper.
            */
            resultObj = [NSMutableArray arrayWithCapacity:[obj count]];
            for (id arrayObj in obj)
            {
                if ([arrayObj isKindOfClass:NSDictionary.class])
                    [resultObj addObject:[NSMutableDictionary dictionaryWithLowercaseKeysFromDictionary:arrayObj]];
                else if ([arrayObj isKindOfClass:NSArray.class])
                    [resultObj addObject:[NSMutableDictionary arrayWithLowercaseKeysForDictionaryArray:arrayObj]];
                else
                    [resultObj addObject:arrayObj];
            }
        }
        else
        {
            // The object is not an NSDictionary or NSArray so keep the object as is
            resultObj = obj;
        }

        // The result object has been converted and can be added to the dictionary. Note this object may be nested inside a larger dictionary.
        [resultDict setObject:resultObj forKey:[key lowercaseString]];
    }];
    return resultDict;
}

/*
 Convienience method to create a new dictionary object with all lowercase keys from an existing instance
 */
- (NSMutableDictionary *)dictionaryWithLowercaseKeys
{
    return [NSMutableDictionary dictionaryWithLowercaseKeysFromDictionary:self];
}

#pragma mark - Private helpers

/*
 Convert NSDictionary keys to lower case when embedded in an NSArray
 */
+ (NSMutableArray *)arrayWithLowercaseKeysForDictionaryArray:(NSArray *)dictionaryArray
{
    NSMutableArray *resultArray = [NSMutableArray arrayWithCapacity:[dictionaryArray count]];
    for (id eachObj in dictionaryArray)
    {
        if ([eachObj isKindOfClass:NSDictionary.class])
            [resultArray addObject:[NSMutableDictionary dictionaryWithLowercaseKeysFromDictionary:eachObj]];
        else if ([eachObj isKindOfClass:NSArray.class])
            [resultArray addObject:[NSMutableDictionary arrayWithLowercaseKeysForDictionaryArray:eachObj]];
    }
    return resultArray;
}

@end

我要說創建第二本字典。 從屬性集加載后,循環遍歷該字典並將對象插入到第二個字典中。 隨時將鍵轉換為小寫。 然后發布第一本字典。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM