Get object from array with NSDictionary objects

Peter

I need to be able to display an object value no matter what the key is. Or display an object with the objects key.

    NSMutableArray *array = [NSMutableArray array];
    NSMutableDictionary *dict= [[NSMutableDictionary alloc] init];

    [dict setObject:[NSNumber numberWithInt:10] forKey:@"key"];
    [array addObject:dict];

    NSLog(@"Key: %@", [[array objectAtIndex:0] key]);
    NSLog(@"Value: %@", [[array objectAtIndex:0] valueForKey:[[array objectAtIndex:0] key]]);

    //Both logs "[__NSDictionaryM key]: unrecognized selector sent to instance"

Why doesn't this work? And how should I do it?

NOTE: In this example the keyValue is "key", BUT I don't know the keyValuein the original code..

Akhilrajtr
NSMutableArray *array = [NSMutableArray array];

NSMutableDictionary *dict= [[NSMutableDictionary alloc] init];
[dict setObject:[NSNumber numberWithInt:10] forKey:@"key"];
[array addObject:dict];

Try

NSDictionary *dict = [array objectAtIndex:0];
NSArray *values = [dict allValues];
NSArray *keys = [dict allKeys];

and log by

NSLog(@"Keys: %@", keys);
NSLog(@"Values: %@", values);

also

for (id key in keys) {

    NSLog(@"Value: %@ for Key: %@", [dict objectForKey:key], key);
}

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

how is Array object different from other objects

분류에서Dev

Get float value out from NSDictionary

분류에서Dev

How to get object from array in swift

분류에서Dev

JQuery : Get a value from the result object array

분류에서Dev

php array of objects can't get object property (Object of class stdClass could not be converted to string)

분류에서Dev

Changing object at NSMutable Array of Objects

분류에서Dev

Populating Object array with new Objects

분류에서Dev

How to get array object in object

분류에서Dev

How to get array of objects from current collection and data from another collection in same query

분류에서Dev

How to access attributes of php object inside a array of php objects from javascript

분류에서Dev

Adding the values from an object to an array of objects without overriding existing key values

분류에서Dev

Remove object from array

분류에서Dev

JSON object expecting an array of objects but gets nothing

분류에서Dev

Typescript literal object containing an array of objects

분류에서Dev

How to get all values of objects inside array

분류에서Dev

jquery get one object in an array as an array

분류에서Dev

filter an array of objects based on an object with an array as it's property value in react

분류에서Dev

Given array of parameters, make array from array of objects

분류에서Dev

Keep getting [object, Object] from array for $.each

분류에서Dev

Remove object from JSON array

분류에서Dev

store data in nsdictionary from uitableview

분류에서Dev

Make json from array of objects javascript

분류에서Dev

To get view object from UITableviewCell

분류에서Dev

Unable to get data from object

분류에서Dev

using LINQ to get a particular set of objects from a list of objects

분류에서Dev

Reduce JSON object containing JSON objects into an array of the form [Path, Value]

분류에서Dev

Sorting array of objects based on data in the object, and nesting them

분류에서Dev

Custom Object properties - multiple primitive variables or array of objects

분류에서Dev

How to flatten an array of objects into arrays for each object parameter?

Related 관련 기사

  1. 1

    how is Array object different from other objects

  2. 2

    Get float value out from NSDictionary

  3. 3

    How to get object from array in swift

  4. 4

    JQuery : Get a value from the result object array

  5. 5

    php array of objects can't get object property (Object of class stdClass could not be converted to string)

  6. 6

    Changing object at NSMutable Array of Objects

  7. 7

    Populating Object array with new Objects

  8. 8

    How to get array object in object

  9. 9

    How to get array of objects from current collection and data from another collection in same query

  10. 10

    How to access attributes of php object inside a array of php objects from javascript

  11. 11

    Adding the values from an object to an array of objects without overriding existing key values

  12. 12

    Remove object from array

  13. 13

    JSON object expecting an array of objects but gets nothing

  14. 14

    Typescript literal object containing an array of objects

  15. 15

    How to get all values of objects inside array

  16. 16

    jquery get one object in an array as an array

  17. 17

    filter an array of objects based on an object with an array as it's property value in react

  18. 18

    Given array of parameters, make array from array of objects

  19. 19

    Keep getting [object, Object] from array for $.each

  20. 20

    Remove object from JSON array

  21. 21

    store data in nsdictionary from uitableview

  22. 22

    Make json from array of objects javascript

  23. 23

    To get view object from UITableviewCell

  24. 24

    Unable to get data from object

  25. 25

    using LINQ to get a particular set of objects from a list of objects

  26. 26

    Reduce JSON object containing JSON objects into an array of the form [Path, Value]

  27. 27

    Sorting array of objects based on data in the object, and nesting them

  28. 28

    Custom Object properties - multiple primitive variables or array of objects

  29. 29

    How to flatten an array of objects into arrays for each object parameter?

뜨겁다태그

보관