2014-06-10 187 views
1

我有位於/Users/admin/Desktop/wi-fi.txt一個文件,其中包含有如下... 的Wi-Fi:查找鍵/值嵌套的NSDictionary

Software Versions: 
     CoreWLAN: 4.3.2 (432.47) 
     CoreWLANKit: 3.3.2 (332.36) 
     Menu Extra: 9.3.2 (932.35) 
     System Information: 9.0 (900.8) 
     IO80211 Family: 6.3 (630.35) 
     Diagnostics: 3.0 (300.40) 
     AirPort Utility: 6.3.2 (632.3) 
    Interfaces: 
    en1: 
     Card Type: AirPort Extreme (0x14E4, 0x10E) 
     Firmware Version: Broadcom BCM43xx 1.0 (5.106.98.100.22) 
     MAC Address: a8:86:dd:a9:6d:13 
     Locale: APAC 
     Country Code: IN 
     Supported PHY Modes: 802.11 a/b/g/n 
     Supported Channels: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 36, 40, 44, 48, 52, 56, 60, 64, 149, 153, 157, 161, 165 
     Wake On Wireless: Supported 
     AirDrop: Supported 
     Status: Connected 
     Current Network Information: 
     DDL2: 
      PHY Mode: 802.11n 
      BSSID: c8:d7:19:62:1e:46 
      Channel: 6 
      Country Code: IN 
      Network Type: Infrastructure 
      Security: WPA2 Personal 
      Signal/Noise: -59 dBm/-86 dBm 
      Transmit Rate: 52 
      MCS Index: 11 
     Other Local Wi-Fi Networks: 
     DDL: 
      PHY Mode: 802.11g 
      BSSID: 00:1b:2f:df:04:bc 
      Channel: 10 
      Network Type: Infrastructure 
      Security: WEP 
      Signal/Noise: -57 dBm/-84 dBm 
     DDL3: 
      PHY Mode: 802.11n 
      BSSID: 28:c6:8e:dc:c8:db 
      Channel: 3 
      Network Type: Infrastructure 
      Security: WPA2 Personal 
      Signal/Noise: -56 dBm/-85 dBm 
     DDL4: 
      PHY Mode: 802.11n 
      BSSID: 40:16:7e:a2:3c:b4 
      Channel: 149,+1 
      Network Type: Infrastructure 
      Security: WPA2 Personal 
      Signal/Noise: -50 dBm/-92 dBm 
     DDL4: 
      PHY Mode: 802.11n 
      BSSID: 40:16:7e:a2:3c:b0 
      Channel: 6 
      Network Type: Infrastructure 
      Security: WPA2 Personal 
      Signal/Noise: -47 dBm/-81 dBm 
     NETGEAR: 
      PHY Mode: 802.11g 
      BSSID: 00:24:b2:bc:32:62 
      Channel: 1 
      Network Type: Infrastructure 
      Security: WEP 
      Signal/Noise: -84 dBm/-84 dBm 
     RS001: 
      PHY Mode: 802.11g 
      BSSID: b0:48:7a:c9:d8:f6 
      Channel: 11 
      Country Code: IN 
      Network Type: Infrastructure 
      Security: WPA Personal 
      Signal/Noise: -44 dBm/-90 dBm 
     RS002: 
      PHY Mode: 802.11g 
      BSSID: b0:48:7a:c9:d9:04 
      Channel: 12 
      Country Code: IN 
      Network Type: Infrastructure 
      Security: WPA Personal 
      Signal/Noise: -51 dBm/-90 dBm 
     RS003: 
      PHY Mode: 802.11g 
      BSSID: 00:1f:33:b4:99:2c 
      Channel: 11 
      Network Type: Infrastructure 
      Security: WPA Personal 
      Signal/Noise: -67 dBm/-90 dBm 
     fortinet_captive_portal: 
      PHY Mode: 802.11n 
      BSSID: 1a:5b:0e:39:57:b2 
      Channel: 6 
      Country Code: IN 
      Network Type: Infrastructure 
      Security: None 
      Signal/Noise: -85 dBm/-81 dBm 
     testlab: 
      PHY Mode: 802.11n 
      BSSID: 08:5b:0e:39:57:b2 
      Channel: 6 
      Country Code: IN 
      Network Type: Infrastructure 
      Security: WPA/WPA2 Personal 
      Signal/Noise: -83 dBm/-81 dBm 

我想要獲得有關每個WiFi網絡名稱及其噪音/強度的信息。任何機構都可以告訴我該怎麼做。

+0

難道你不應該問是否有API來獲取此信息,而不是使用這種笨拙,緩慢和脆弱的解決方案嗎?或者您是否已經嘗試查找API並失敗? – trojanfoe

+0

我知道API objectForKey,但我不知道如何在嵌套的情況下使用它。 –

回答

1

不知道我是否理解這個問題。試着讓它更清楚一點。

無論如何,這是我的理解。

NSString *path = [[NSBundle mainBundle] pathForResource:@"/Users/admin/Desktop/wi-fi" ofType:@"txt"]; 
NSArray *categories = [NSArray arrayWithContentsOfFile:path]; 
for (NSDictionary *category in categories) { 
    NSLog(@"%@", [category objectForKey:@"Interfaces"]); 
    NSLog(@"%@", [category objectForKey:@"Software Versions"]); 
} 

我還沒跑這個,但我認爲這應該工作,給它一個鏡頭。希望這是類似於你正在尋找的東西。如果不是,希望它朝着正確的方向發展,並且讓你更接近找到答案。快樂的編碼!

編輯

拖動文本文件到您的項目,然後執行以下

NSString *path = [[NSBundle mainBundle] pathForResource:@"nsdictionarytextfile" ofType:@"txt"]; 
NSString *content = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:NULL]; 
NSArray *testArray = [content componentsSeparatedByString:@"\n"]; 
NSString *topDictionary = @""; 
NSMutableDictionary *dict = [NSMutableDictionary dictionary]; 
NSMutableDictionary *finalDict = [NSMutableDictionary dictionary]; 
for(NSString *s in testArray){ 
    NSArray *newArray = [s componentsSeparatedByString:@":"]; 
    if([newArray[1] isEqualToString:@""]){ 
     if([dict count] == 0){ 
      topDictionary = newArray[0]; 
     } 
     else{ 
      topDictionary = [topDictionary stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; 
      [finalDict setObject:[dict copy] forKey:[topDictionary copy]]; 
      [dict removeAllObjects]; 
     } 
    } else{ 
     NSString *object = [newArray[1] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; 
     NSString *key = [newArray[0] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; 
     [dict setObject:object forKey:key]; 
    } 
} 
topDictionary = [topDictionary stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; 
[finalDict setObject:dict forKey:topDictionary]; 
NSLog(@"finalDict: %@", finalDict); 

*請不說這還沒完!你必須找出更多的邏輯*

不真正知道你想要他們,但這應該讓你看看它應該如何工作。我沒有弄清楚添加頂級字典及其子級字典的完整邏輯,但是這應該使您處於正確的方向。我可能會首先刪除放在頂部詞典中的裁剪,並檢查字符串前面的空白/空格,然後用它來確定字典應該放在哪裏。 IE沒有空的空間意味着它是最高的一個,而更多的空白是它在字典中的「內在」。希望這可以幫助你開始。

+0

For循環沒有得到執行 –

+1

這是因爲'NSBundle pathForResource'不是打開該文件的方式。該文件顯然不是應用程序包的一部分,因此爲什麼會涉及「NSBundle」。此外,該文件不是一個數組。 – trojanfoe

+0

@trojanfoe你是對的。 OP將不得不添加文件才能使用nsbundle打開文件。 – Chris

0

您只有文本文件作爲輸入。這不是一個嵌套的字典。您應該將文件作爲文本讀取並首先進行解析。

該文件的格式看起來像YAML,但此時它不是有效的YAML。如果您有權訪問生成該文件的代碼,那麼確保它生成有效的YAML是有意義的。然後,您可以在Objective-C中使用one of available libraries解析YAML文件以嵌套NSDictionary s。否則,您應該爲輸入文本文件編寫自己的解析器。