0

I have an NSString that returns a list of values like this:

test_1=value_1  
test/2=value_2  
test3=value_3 value_4  
test_4=value_5/value_6  
...  

More realistic result values:

inameX=vlan2    
hname=server    
lanipaddr=192.168.1.1    
lannetmask=255.255.255.0    
islan=0    
islwan=0    
dhcplease=604800    
dhcplease_1=302400  
ct_tcp_timeout=0 1200 40 30 60 60 5 30 15 0
ct_timeout=10 10
ct_udp_timeout=25 60
ctf_disable=1    
ddnsx0=
cifs2=0<\\192.168.1.5
and so on...   

If I do:

 for (id key in dict) {
            NSLog(@"key: %@, value: %@", [dict objectForKey:key], key);
        }

it outputs:

key: inameX, value: vlan2
key: hname value: server    
key: lanipaddr value: 192.168.1.1    
key: lannetmask value: 255.255.255.0 

This list is stored in one NSString *result. Not sure if I should put it in an array for this but I need to be able to call a function or command that will return a specific value_X based on the argument to match the variable. For example, get value of test_1 variable then it would return value_1. Or get test_4 then it would return value_5/value_6

Any idea how I can do that?

I appreciate your help. Thanks!

3
  • 1
    Sounds like a homework assignment. Can you be more clear on what you're trying to do and I'll try to lay out the steps on how to accomplish it? What exactly are you inputs and outputs? Are you supposed to parse the string for operators (*/-+)? How do you determine when one test ends and the next begins? Commented Jul 31, 2013 at 3:19
  • Not a homework assignment. I am just trying to learn to interact to a Linux server and get values by executing commands. The output of a specific command would show as I wrote initially to an NSString. I just want to be able to say get value of test_1 so that i can output it to a UILabel value_1 I send NSString *result = [ConnectIt executeIt:TextBox.text]; The result shows as list above. values could be integer or some name Commented Jul 31, 2013 at 3:41
  • @JeffCompton any help to parse the list up there would be appreciated. Commented Aug 2, 2013 at 2:03

3 Answers 3

2

You probably want the method in NSString called componentsSeparatedByCharactersInSet: to split up that one string into an array. Since your values are separated by '=' and new line characters ('\n'), you want the set to include those two characters:

NSArray *strings = [NSString componentsSeparatedByCharactersInSet:
                        [NSCharacterSet characterSetWithCharactersInString:@"=\n"]];

And then you can make this into a dictionary with NSDictoinary's dictionaryWithObjects: AndKeys: But first, you need to split that array into two arrays; one with objects, one with keys:

 NSMutableArray *keys = [NSMutableArray new];
    NSMutableArray *values = [NSMutableArray new];
    for (int i = 0; i < strings.count; i++) {
        if (i % 2 == 0) { // if i is even
            [keys addObject:strings[i]];
        }
        else {
            [values addObject:strings[i]];
        }
    }

Then you put them into an NSDictonary

NSDictionary *dict = [NSDictionary dictionaryWithObjects:values forKeys:keys];
NSLog(@"%@", dict[@"test_1"])  // This should print out 'value_1'

Hope that helps!

Sign up to request clarification or add additional context in comments.

9 Comments

You are right, I overlooked which object that method uses, fixed that. However I was creating an NSSet with an object, @['=','\n'] is an NSArray object.
This doesn't work. I get bad exec. NSString *result = [ConnectIt executeIt:TextBox.text]; NSArray *strings = [result componentsSeparatedByCharactersInSet: [NSCharacterSet characterSetWithCharactersInString:@"=\n"]]; NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:strings]; NSLog(@"%@", dict[@"test_1"]);
The last edit should work for you. I forgot you can't put an array in dictionaryWithObjectsAndKeys:.
I ran once and it gave me the right value based on dict[@"test_1"]. I changed that to dict[@"test_2"] but it output (null). Any idea?
Sample list output from result: inameX=vlan2 lanipaddr=192.168.1.1 islan=0 dhcplease=604800 I want to be able to say get me the value of inameX then output is vlan2 If I ask for dhcplease then output is 604800
|
0

Use an NSDicationary. NSDictionaries are key value stores. In other words, there are a list of keys. Each key is unique. Each key has an associated value. The value can be any data type and the key has to conform to the NSCopying protocol (typically an NSString). If you try to access the value for a key that doesn't exist in your NSDictionary, the return value will be nil.

//create the dictionary and populate it
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
[dict setObject:@"value_1" forKey:@"key_1"];
[dict setObject:@"value_2" forKey:@"key_2"];
[dict setObject:@"value_3" forKey:@"key_3"];
[dict setObject:@"value_4" forKey:@"key_4"];


NSString *stringInput = [self getStringInput];//however you find out your input

//find your string value based on the key passed in
NSString *strValue = [dict objectForKey:stringInput];

6 Comments

I could use that but these values changes dynamically meaning test_1 would have value_1 now but value_8 in 5 minutes from now if I reload the data. Basically everytime I reload NSString *result it will have a list with the same variables but with different values.
@Squirrel NSMutableDictionary... I don't see why an array is better for implementing mutable dictionaries.
I think you mean setObject:forKey:, not setValue:forKey:, and the key doesn't have to be an NSString; it just has to conform to the NSCopying protocol.
@AaronBrager setValue:forKey: is just fine.
I was curious what the difference was. For anyone else that is also curious: stackoverflow.com/questions/1249634/…
|
0

You can use a NSScanner to make this work.

Scan for the string for which you want the value, and then scan the string until you encounter \n and then use it for your requirement.

NSScanner *scan =[NSScanner scannerWithString:theString];
[scan scanString:keyString inToString:nil];
[scan setScanLocation:[scan scanLocation]+1];

[scan scanString:@"\n" inToString:requiredString];

So requiredString is the string which you want.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.