Retrieving Data from an NSDictionary

I'm not quite sure that I understand how to do this.

If I wanted to add, for example, the author and the name (key, value) for the dictionary. Fill this dictionary with data, then enter another dictionary, for example, for example. k = genre v = artist name and fill it with data.

Then I want to add dictionaries to the array.

First, how is this done? and secondly, what if I allow the user to register their own records. Thus, I have to have text fields on the screen, when the user is executed, he saves the fields as a pair of key values ​​in a new dictionary, for example. user dictionary.

What will I do later, trying to fill out a table with user data entered, I do not know the keys or values ​​in this dictionary, since I can get this data?

I mean, let's say I want to load a user dictionary from the index of array 2 and populate the tableview cells with each dictionary entry, how is this done? Maybe a method like an array (get entry.title by index blah blah), get the value Key in the dictionary?

How else can I load user-loaded data that they know about values?

Hi

+3
source share
2 answers

( , - , NSString ):

NSMutableDictionary *books = [[NSMutableDictionary alloc] initWithCapacity:10];
[books setObject:title forKey:author];

/:

NSMutableDictionary *music = [[NSMutableDictionary alloc] initWithCapacity:10];
[books setObject:artist forKey:genre];

:

NSArray *theArray = [NSArray arrayWithObjects:books, music, nil];

, 2:

id key;
NSDictionary *theDictionary = [theArray objectAtIndex:2];
id value;
for (key in [theDictionary allKeys])
{
    value = [theDictionary objectForKey:key];
    // do something with the value
}
+8

NSArray , allKeys. , objectForKey:

for (id key in [myDict allKeys]) {
    NSLog(@"key: %@, value: %@", key, [dictionary objectForKey:key]);
}
+3

Source: https://habr.com/ru/post/1753745/


All Articles