简体   繁体   中英

NSArray valueForKey ObjectAtIndex

I have an NSArray that i want a single value of.

_membersArray = [_detailItem valueForKey:@"members"];

When i use this code:

NSLog(@"%lu", (unsigned long)[[_membersArray valueForKey:@"name"] count ]);

i get: 1

When i use this one:

    NSLog(@"%@", [_membersArray valueForKey:@"name"] );

i get

{(
    "Name Lastname"
)}

But i need only "Name lastname"

And when i use:

NSLog(@"%@", [[_membersArray valueForKey:@"name"] objectAtIndex:0]);

i get:

[__NSSetI objectAtIndex:]: unrecognized selector sent to instance 0x10bf5caf0
2014-03-13 12:35:39.261 Givem[2454:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSSetI objectAtIndex:]: unrecognized selector sent to instance 0x10bf5caf0'

i guess this is a easy one but i have a codeblock. Thanks for helping me out

然后尝试这个..

[[[_membersArray valueForKey:@"name"] allObjects]objectAtIndex:0];

Look at exception ... It says ...

[__NSSetI objectAtIndex:] ...

... it means, that you're trying to send objectAtIndex: message to NSSet , not NSArray . And NSSet doesn't have objectAtIndex: method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM