I have seen plenty on how to fetch dictionaries and things from plists but how do I specify specifically which array I want to grab from my plist?
E.g. I have two arrays in a plist, one called array1 and another called array2 how do I end up with two NSArrays?
Thanks.
In a .plist file your root object is either an array or a dictionary. If you say, you have 2 arrays in your plist, called array1 and array2, that means your root object is a dictionary. So you load your plist into an NSDictionary and access your arrays like this:
NSDictionary *myPlistRoot = [NSDictionary dictionaryWithContentsOfFile:plistPath];
NSArray *array1 = [myPlistRoot objectForKey:#"array1"];
NSArray *array2 = [myPlistRoot objectForKey:#"array2"];
The arrays will just be contained within the dictionary, so you get them out the same way you get any object out of a dictionary:
NSArray *array1 = [someDictionary objectForKey:#"array1"];
Related
I know how I would achieve this using NSMutableArray, but whats the correct way of emptying a whole array of class NSArray. I need to do this because I need to reload a tableView. Im using ARC.
NSArray is an immutable type. You cannot alter it's contents after creation.
Either use an NSMutableArray or replace it with a new (empty) NSArray.
NSArray *yourArray = [ whatever objects you have ]
//to empty this array
yourArray = [NSArray array];
NSArray is an immutable (unchangeable) class so there is no way to remove elements from the array. Basically, you will have to throw the array away and replace it with a new NSArray. Alternatively, you could just use an NSMutableArray.
You cant empty a non mutable NSArray, the best approach is to get a mutable copy of your array:
NSMutableArray *arr=[yourArr mutableCopy];
[arr removeAllObjects];
I create two NSMutableDictionary: dictionary1 and dictionary2. In the first dictionary I store some array object having there keys. And in the second dictionary I store the object of first dictionary with the key like this:
int page = 1;
[dictionary2 setObject:dictionary1 forKey:[NSString stringWithFormat:#"%d",page]];
[dictionary1 removeAllObjects];
but at the time of retrieve of the object i do like this
dictionary1 = [dictionary2 valueForKey:[NSString stringWithFormat:#"%d",page]];
NSLog(#"dictionary1 %#", dictionary1);`
Then it gives null value. I don't know what mistake I do.
After you add dictionary1 to dictionary2, you are removing all of the objects.
This is the same dictionary that is in dictionary2 (it does not create a copy), therefore you are removing the objects from it as well.
You need to remove the line [dictionary1 removeAllObjects];.
Then, since you are done with dictionary1 at this point, you can either remove the reference to it or set to a nice new shiny dictionary which is ready to use:
// Remove the reference
dictionary1 = nil;
// Or, create a new, empty dictionary
dictionary1 = [[NSDictionary alloc] init];
Did you correctly alloc/init dictionary1 and dictionary2? Make sure the dictionaries themselves are not nil.
try this
dictionary1 = [dictionary2 objectForKey:[NSString stringWithFormat:#"%d",page]];
I have NSMutableArray with 10,000 objects, each object has Name, Details, ...etc.
I want to Create an NSArray Which contains ONLY the Names of all objects.
Use KVC
NSArray *names = [myArray valueForKey:#"name"];
Check the docs for NSArray
valueForKey:
Returns an array containing the results of invoking valueForKey: using key on each of the array's objects.
I have a dictionary "playerDict" that reads data from a plist where there is names (myKey) with nine associated objects to each key.
I am trying to read all objects for a specific key (myKeys) into an NSMutableArray (theObjects). I have read the class reference and search internet but cannot figure our this, probably very simple, problem.
Among all other test i have done I have tried the following but that returns the key into theObjects and not the objects.
theObjects = [playerDict objectForKey:myKeys];
Anyone that could give a hint?
Here is the code that created the dict, i stripped it:
NSArray *objs = [NSArray arrayWithObjects:[NSNumber numberWithBool:playerObject.diffHard],[NSNumber numberWithBool:playerObject.diffMedium],
[NSNumber numberWithBool:playerObject.diffEasy],[NSNumber numberWithBool:playerObject.currentGame],
[NSNumber numberWithInt:playerObject.currentGameQuestion],[NSNumber numberWithInt:playerObject.currentGameRightAnswer],
[NSNumber numberWithInt:playerObject.currentGameType],[NSNumber numberWithInt:playerObject.nrOfType0Games],
[NSNumber numberWithInt:playerObject.type0Result], nil];
NSDictionary *newPlayerDict = [NSDictionary dictionaryWithObjectsAndKeys:objs, keyString, nil];
Try valueForKey:
You can only store one item per key in an NSDictionary. If you need story multiple items for the same key, you need to first add each of the items to an NSArray (or NSSet) that you instead then set as an object in your dictionary.
If might be useful if you posted the code that creates the dictionary.
Update: It looks like you are already doing this. So:
NSArray *myObjs=[playerDict objectForKey:keyString];
will get you your array. And this:
BOOL diffHard=[[myObjs objectAtIndex:0] boolValue];
BOOL diffMedium=[[myObjs objectAtIndex:1] boolValue];
Will get you the value you stored in the first and second objects of the array. Repeat it for the rest.
i want to get unique strings from an array and to store those strings into another array..... give logic or sample code for this ...............thanks in advance
NSArray *uniqueArray = [[NSSet setWithArray: yourArray] allObjects];