簡體   English   中英

存儲許多NSDictionaries的最佳方法是什么?

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

我需要在iPhone應用程序中存儲許多Facebook App IDTwitter消費者密鑰以用於不同的配置和設置。

我知道我這樣做的方法不是最好的方法,因為它包含太多if else語句。 使用正確的Objective-C設計模式完成此任務的最佳方法是什么?

這是我的代碼:

- (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];

}

兩個主要問題:

  • 為什么鍵名在數組中?
  • 您不應該將NSString * s與==進行比較。

您可能還想使用語言代碼。

我可以想到兩個“ Objective-C”選項,一個“ hacky”選項和一個“ C”選項:

  1. 將其存儲在字典中。 這似乎有點浪費,因為它構造了一堆字典並且僅使用其中一個。

     [NSDictionary dictionaryWithObjectsAndKeys: [NSDictionary dictionaryWithObjectsAndKeys: @"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", @"facebookAppId", @"yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy", @"facebookLocalAppId", ... nil], @"en", [NSDictionary dictionaryWithObjectsAndKeys: @"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", @"facebookAppId", ... nil], @"es", ... nil] 
  2. 將其粘貼在plist中的字典中。 這可能是有問題的,因為提取文件所需的所有工作都是解壓縮.ipa。

     <dict> <key>en</key> <dict> <key>facebookAppId</key> <string>...</string> ... </dict> <key>es</key> <dict> ... </dict> ... </dict> 
  3. 將其存儲在Localizable.strings中。 再次,通過解壓縮.ipa來進行提取很簡單。

  4. 將其存儲在結構數組中:

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

那么數據必須存儲在正確的地方嗎? 您可以有一個單獨的類,將數據存儲在哈希表中。 然后,您只需要在每次使用某種語言時都引用該功能即可。 您的代碼肯定看起來更干凈。 簽出NSHashTable。

暫無
暫無

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

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