8

I am having two arrays, Namely

NMutableArray* first;
NMutableArray* second;

Now I am copying first object to the second array like

for (int i=0;i<first.count; i++)
{
 [second addObject:[first objectAtIndex:i];
}

This is ok. I don't know how to access the value of the First Array. I tried like this ,

[second addObject:[[first objectAtIndex:i]name]];

I want to get the name value which is in the first object of first array. I tried using the above line, it is showing some warning. Please help me

3 Answers 3

7

Assuming you started with an array like this:

NSArray *array1 = @[@{@name : @"Fred"},
                    @{@name : @"Bill"}];

You could create a second array that contains the value of a given property of each element of the first array as follows:

NSArray *array2 = [array1 valueForKey:@"name"];

If you then logged the second array...

NSLog(@"%@", array2);

...the resulting output would be

2012-04-18 16:26:11.226 ExampleRunner[23320:707] (
    Fred,
    Bill
)

EDIT

Note that this will work regardless of whether the objects in the first array are instances of NSDictionary as shown in the example above, or instances of a class or classes that have a name property or instance variable (or an _name instance variable, for that matter). For more information on how and why this works, see the documentation for the NSKeyValueCoding informal protocol:

http://developer.apple.com/library/ios/#DOCUMENTATION/Cocoa/Reference/Foundation/Protocols/NSKeyValueCoding_Protocol/Reference/Reference.html

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

2 Comments

Hi @jlehr, I am not sure how many objects I will add in the First array. In that case ,I think Key value will be not suitable. Right ?
@user562100 I'm not sure I understood your question. Can you please clarify?
3

The brackets are currently in the wrong place:

[second addObject:[[first objectAtIndex:i] name]];

4 Comments

+1 to your answer because I didn't notice that the OP's brackets on that one line are all messed up. Sheesh
I am really sorry that I have wrongly typed those tags in the question.Now I have updated it.
@user562100 you still have wrong bracketing in your updated question. I see three "[" characters and only two "]" characters.
Oh cool. I have changed it again. Any idea how to get the value ?
1

Updated Answer:

Again, I think you should split stuff out into easy to parse lines of code:

for (id theObject in first)
{
    // without an actual type, I still think the compiler might
    // throw a warning on this next line of code; 
    // but maybe RJR III is correct and it won't warn. 
    // I didn't check.
    NSString * nameOfObject = [theObject name];
    if(nameOfObject)
    {
        [second addObject:nameOfObject];
    }
}

Notice that I do some error checking in here as well (i.e. making sure the name is not nil).

Original Answer:

You're getting a warning because the compiler doesn't know what kind of custom object is being fetched from your call to "[first objectAtIndex: i]". In other words, it doesn't know what kind of object you're trying to get the "name" of.

Cast it to the right type and you'll get rid of the warning.

Or even better, split that one line of multiple things happening at once into two or three lines of code and make your code more readable in the process.

2 Comments

-objectAtIndex: returns an id, so casting to the correct type is unnecessary, even with the strictness of ARC.
If I am trying to access the object using[second addObject:[first objectAtIndex:i]]; it is working. But I want to access the particular value of in the first object of the First array.

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.