简体   繁体   English

存储许多NSDictionaries的最佳方法是什么?

[英]What's the best way to store many NSDictionaries?

I need to store many Facebook App Id's and Twitter Consumer Keys for differente configurations and settings in my iPhone application. 我需要在iPhone应用程序中存储许多Facebook App IDTwitter消费者密钥以用于不同的配置和设置。

I know the way I'm doing this is not the best one, because it has too many if else statements. 我知道我这样做的方法不是最好的方法,因为它包含太多if else语句。 What would be the best way to accomplish this task, using a correct Objective-C design pattern? 使用正确的Objective-C设计模式完成此任务的最佳方法是什么?

Here is my code: 这是我的代码:

- (id) init {
self = [super init];
if (!self) return nil;

keyNames = [[NSArray arrayWithObjects:
             @"facebookAppId", 
             @"facebookLocalAppId",
             @"twitterConsumerKey",
             @"twitterSecret",
             nil]retain];

if (MainLanguage == @"English") {

    keys = [[NSArray arrayWithObjects:
             @"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", //facebookAppId
             @"yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy", //facebookLocalAppId
             @"zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz", //twitterConsumerKey
             @"wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww", //twitterSecret
             nil]retain];

    dictionary = [[NSDictionary dictionaryWithObjects:keys forKeys:keyNames]retain];

}

else if (MainLanguage == @"Spanish") {

    keys = [[NSArray arrayWithObjects:
             @"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", //facebookAppId
             @"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", //facebookLocalAppId
             @"cccccccccccccccccccccccccccccccc", //twitterConsumerKey
             @"dddddddddddddddddddddddddddddddd", //twitterSecret
             nil]retain];

    dictionary = [[NSDictionary dictionaryWithObjects:keys forKeys:keyNames]retain];

}  else if (MainLanguage == @"French") {

   //etc...

} else if (MainLanguage == @"Italian") {

    //etc...

}


//etc..

return self;

} }

-(void)dealloc {

    [keyNames release];
    [keys release];
    [dictionary release];
    [super dealloc];

}

Two main problems: 两个主要问题:

  • Why are the key names in an array? 为什么键名在数组中?
  • You should not be comparing NSString*s with ==. 您不应该将NSString * s与==进行比较。

You also probably want to be using language codes. 您可能还想使用语言代码。

I can think of two "Objective-C" options, one hacky option, and one "C" option: 我可以想到两个“ Objective-C”选项,一个“ hacky”选项和一个“ C”选项:

  1. Store it in a dictionary. 将其存储在字典中。 This seems a little wasteful because it constructs a bunch of dictionaries and only uses one of them. 这似乎有点浪费,因为它构造了一堆字典并且仅使用其中一个。

     [NSDictionary dictionaryWithObjectsAndKeys: [NSDictionary dictionaryWithObjectsAndKeys: @"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", @"facebookAppId", @"yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy", @"facebookLocalAppId", ... nil], @"en", [NSDictionary dictionaryWithObjectsAndKeys: @"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", @"facebookAppId", ... nil], @"es", ... nil] 
  2. Stick it a dictionary in a plist. 将其粘贴在plist中的字典中。 This might be problematic, because all someone has to do to extract them is unzip your .ipa. 这可能是有问题的,因为提取文件所需的所有工作都是解压缩.ipa。

     <dict> <key>en</key> <dict> <key>facebookAppId</key> <string>...</string> ... </dict> <key>es</key> <dict> ... </dict> ... </dict> 
  3. Store it in Localizable.strings. 将其存储在Localizable.strings中。 Again, trivial to extract by unzipping the .ipa. 再次,通过解压缩.ipa来进行提取很简单。

  4. Store it in an array of structs: 将其存储在结构数组中:

     struct { NSString *lang; NSString *facebookAppId; NSString *facebookLocalAppId; NSString *twitterConsumerKey; NSString *twitterConsumerSecret; } const foo_data[] = { {@"en", @"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", @"yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy", ...}, {@"es", @"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", ...}, ... }; ... for (size_t i = 0; i < sizeof(foo_data)/sizeof(foo_data[0]); i++) { if ([lang isEqualToString:foo_data[i].lang]) { return [NSDictionary dictionaryWithObjectsAndKeys: foo_data[i].facebookAppId,@"facebookAppId", ... nil]; } } 

Well the data has to be stored somewhere right? 那么数据必须存储在正确的地方吗? You could have a separate class that stores the data in a hash table. 您可以有一个单独的类,将数据存储在哈希表中。 Then you just need to reference that able each time you get a language. 然后,您只需要在每次使用某种语言时都引用该功能即可。 Your code will definitely look cleaner. 您的代码肯定看起来更干净。 Check out NSHashTable. 签出NSHashTable。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM