objective c - How do would you split this NSString into a NSDictionary? -


i have data aquire linux box , want put nsdictionary later processing.

how wold nsstring nsdictionary following?

data ( eth0 (errors => 0, bytes => 32, packtes => 0, index => 2,...) eth1 (errors => 0, bytes => 32, packtes => 0, index => 2,...) lo (errors => 0, bytes => 32, packtes => 0, index => 2,...) ...) 

each value should accessable like:

nsstring eth0_errors = [[data objectforkey:@"eth0"] objectforkey:@"errors"]; 

the data string looks like:

nsstring *dummydata = [nsstring stringwithformat:@"+ 1\n"                        "eth0 errors 0\n"                        "eth0 bytes 34\n"                        "eth0 packets 0\n"                        "eth0 index 2\n"                        "eth0 type ether\n"                        "eth0 mac 00:0c:29:39:81:9c\n"                        "eth0 mtu 1500\n"                        "eth0 broadcast ff:ff:ff:ff:ff:ff\n"                        "eth0 base-addr 00000000\n"                        "eth0 irq 0\n"                        "eth0 realm intern\n"                        "eth0 flags broadcast\n"                        "eth0 features hw-csum hw-vlan-tx hw-vlan-rx hw-vlan-filter\n"                        "eth0 speed 1000mb/s\n"                        "eth0 duplex full\n"                        "eth0 negotiation on\n"                        "eth0 link up\n"                        "eth0 switch\n"                        "eth1 errors 0\n"                        "eth1 bytes 0\n"                        "eth1 packets 0\n"                        "eth1 index 3\n"                        "eth1 type ether\n"                        "eth1 mac 00:0c:29:39:81:a6\n"                        "eth1 mtu 1500\n"                        "eth1 broadcast ff:ff:ff:ff:ff:ff\n"                        "eth1 base-addr 00000000\n"                        "eth1 irq 0\n"                        "eth1 realm unknown\n"                        "eth1 flags broadcast\n"                        "eth1 features sg/io hw-csum hw-vlan-tx hw-vlan-rx hw-vlan-filter\n"                        "eth1 negotiation on\n"                        "eth1 link down\n"                        "eth1 switch\n"                        "eth1 speed ?\n"                        "eth1 duplex ?\n"                        "lo errors 0\n"                        "lo bytes 79\n"                        "lo packets 1\n"                        "lo index 1\n"                        "lo type loopback\n"                        "lo mac 00:00:00:00:00:00\n"                        "lo mtu 3500\n"                        "lo broadcast 00:00:00:00:00:00\n"                        "lo base-addr 00000000\n"                        "lo irq 0\n"                        "lo realm opsys\n"                        "lo flags loopback\n"                        "lo features sg/io no-csum high-dma fraglist\n"                        "lo link up\n"                        "lo switch\n"                        "lo speed ?\n"                        "lo duplex ?\n"                        "lo negotiation ?\n"                        ".\n"]; 

assuming data compliant format specified, use this:

nsarray *components = [dummydata componentsseparatedbystring:@"\n"]; nsmutabledictionary *dictionary = [nsmutabledictionary new];  for(nsstring *component in components) {     nsarray *subcomponents = [component componentsseparatedbystring:@" "];      if(subcomponents.count >= 3) {         nsmutabledictionary *subdictionary = [dictionary objectforkey:[subcomponents objectatindex:0]];          if(subdictionary == nil) {             subdictionary = [nsmutabledictionary new];             [dictionary setobject:subdictionary forkey:[subcomponents objectatindex:0]];         }          if(subcomponents.count > 3) {             [subdictionary setobject:[subcomponents subarraywithrange:nsmakerange(2, subcomponents.count - 2)] forkey:[subcomponents objectatindex:1]];         } else {             [subdictionary setobject:[subcomponents objectatindex:2] forkey:[subcomponents objectatindex:1]];         }     } } 

note numeric values represented nsstrings. if want them nsnumbers instead, need perform additional checks , conversions within loop.


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -