0

I retrieve 6 values(say name, age, sex, address, id, tag) from a web service. All are string variables. I concatenate these strings and add it to an NSMutableArray. I pass this array to another class, where I need each of these strings separately. That is I need to be able to retrieve these values from the array separately. How can I do this. Do I need to add tags like "Name", "Age" etc along with the values to make the retrieval easier. Whats the appropriate way to do it.

Edit: i concatenate it into a single string. How should I be adding my values to the collection, so that I can retrieve the elements easily.

2
  • Are you concatenating the strings into one NSString? Or are you saying you're inserting the strings as 6 elements in the NSMutableArray? Hard to tell from your question. Commented Mar 26, 2012 at 17:07
  • Each element of my array should contain all these values as separate entities. My array could have a lot of elements. Commented Mar 26, 2012 at 17:22

2 Answers 2

3

IMO, the most appropriate way of doing what you are trying to do is using an NSMutableDictionary, that allows you to access individual elements based on their key.

Example:

loadedBuffers = [[NSMutableDictionary alloc] initWithCapacity:CD_BUFFERS_START];
[loadedBuffers setObject:bufferId forKey:filePath];
...
[loadedBuffers objectForKey:filePath]

You do no strictly need using a dictionary, but it will make your life so much easier.

In your case (if I understand it correctly), I would do:

NSMutableArray* result = [NSArray arrayWithCapacity:kNUM_OF_ROWS];

NSString *name, *age, *sex....;

<for each set of strings from the web service>
    <retrieve strings>
    NSMutableDictionary dict = [NSMutableDictionary dictionaryWithCapacity:kNUM_OF_FIELDS];
    [dict setObject:name forKey:@"name"];
    ...
    [dict setObject:address forKey:@"address"];

    [result addObject:dict];
<end_for>

return result;

By doing like this, you will be able to access sequentially each set of strings; then access each string individually.

In short, instead of encoding your set of strings by concatenating them into another string, you would expand them in a dictionary to make retrieval easier.

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

2 Comments

Will the retrieving part be easier. Could I access each value from the dictionary separately. I think dictionary values can be accessed only by the key. So, not being able to use the index value cause problems. ANd my question is, how should I be adding my values to the collection, so that I can retrieve it easily.
In the above case, what should be my key?
0

I would agree that the best practise here would be to use a dictionary or custom object. That way each string gets stored with its companions (e.g. you have one person's data all together) and you don't have to deal with the messy method you already have implemented. It sounds like you might want to save data, so here's a snippet to help you. If that's not what you're after, let me know and I'll modify my response to help!

Say you have a custom object class Person, where you create and manage data objects to save to disk via the app delegate. You'd do something like:

Person *newPerson = [[Person alloc] init];
[newPerson setName:@"John"];
[newPerson setAge:@"25"];
[newPerson setSex:@"M"];
[yourAppDelegate.newPersonArray insertObject:newPerson atIndex:[mainDelegate.newPersonArray count]];

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.