Faisal Ikwal
Faisal Ikwal

Reputation: 703

Finding keys/Values from nested NSDictionary

I have a file located at /Users/admin/Desktop/wi-fi.txt, which contains are as follows... 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

I want to get information about each wifi network name and its noise / strength.Can any body tell me how to do it.

Upvotes: 1

Views: 184

Answers (2)

Chris
Chris

Reputation: 1034

Not sure if I understand the question. Try to make it a little bit more clear.

Anyway, here's what I think I understood.

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

I haven't ran this but I think this should work, give it a shot. Hopefully this is something similar to what you're looking for. If not, hopefully it's in the right direction and it brings you a step closer to finding your answer. Happy coding!

edit:

Drag the text file into your project and then do the following

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);

* PLEASE NOT THAT THIS IS NOT FINISHED ! YOU WILL HAVE TO FIGURE OUT MORE LOGIC *

Don't really know how you want them but this should give you a look on how it should work. I didn't figure out the complete logic for adding a top level dictionary and its sub level dictionary but this should put you in the right directions. I would probably start by removing the trimming I put in the top dictionary and check the empty/white space in front of the string and use that to figure out where the dictionary is supposed to go. IE no empty space means that it is the highest one and the more white spaces the "inner" it is in the dictionary. Hope this helps you get started.

Upvotes: 1

yas375
yas375

Reputation: 3980

You have just text file as input. It's not a nested dictionary. You should read the file as text and parse it first.

The file's format looks like YAML, but at this point it's not valid YAML. If you have an access to code which generates the file, then maybe it makes sense to make sure it generates valid YAML. Then you could use one of available libraries in Objective-C to parse YAML files to get nested NSDictionarys. Otherwise you should write your own parser for the input text file.

Upvotes: 0

Related Questions