从嵌套的NSDictionary中查找键/值

时间:2014-06-10 06:16:16

标签: objective-c

我有一个位于/Users/admin/Desktop/wi-fi.txt的文件,其中包含如下内容...... 的Wi-Fi:

  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网络名称及其噪音/强度的信息。任何人都可以告诉我该怎么做。

2 个答案:

答案 0 :(得分: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没有空格意味着它是最高的空格,而它在字典中的“内部”空格越多。希望这有助于您入门。

答案 1 :(得分:0)

您只有文本文件作为输入。它不是嵌套字典。您应该将文件作为文本读取并首先解析它。

该文件的格式与YAML类似,但此时它的YAML无效。如果您可以访问生成该文件的代码,那么确保它生成有效的YAML可能是有意义的。然后,您可以使用Objective-C中的one of available libraries来解析YAML文件以获取嵌套的NSDictionary。否则,您应该为输入文本文件编写自己的解析器。