0

我有一些我从一些 linux 机器中获取的数据,并希望将其放入 NSDictionary 以供以后处理。

您如何将此 NSString 放入 NSDictionary 中,如下所示?

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,...)
...)

每个值都应该可以访问,例如:

NSString eth0_errors = [[data objectForKey:@"eth0"] objectForKey:@"Errors"];

数据字符串如下所示:

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 UP 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 UP 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"];
4

1 回答 1

2

假设您的数据将始终符合您指定的格式,您可以使用如下内容:

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

请注意,数值将表示为 NSStrings。如果您希望它们改为 NSNumber,则需要在循环中执行额外的检查和转换。

于 2013-05-19T22:58:27.860 回答