Getting objects from NSArray within another NSArray - ios

for an iOS 5.0 application using ARC, i have an NSArray of objects that contain NSArray of other objects within it. Is it possible to extract a list of objects from inner arrays without iterating through the Array e.g. say, with NSpredicate or valueForKeyPath. To be clearer, I have:
NSArray *objtype1 contains
-id
-NSArray *imageObjs containing imageObjects
-imagetype = 1 <--1st imageObject
imageURL1
-imagetype = 2 <--2nd imageObject
imageURL2
-NSArray *objtype2
-other parameters
I need to extract the NSArray of imageType = 1 imageObjects to pass in for further processing. Is this possible? (I'm looking at NSpredicate, and valueForKeyPath, but have not found anything yet)

I think the reason you haven't found anything yet is because it's not there. You could implement your own category on NSArray to use predicates recursively. Perhaps someone else have done it already. I don't know.

Ummmmm it seems a bit unclear as to what you are doing. It looks like you have an NSArray of multiple types. And you want the imageObjs array inside it.
If you infact have an NSArray like this, it would be monumentally easier to convert it to an NSDictionary. Then you can use [dictionary valueForKey:#"Image Array"]; to get the image array out of it.
Currently, your solution to get the imageObjs array would be [objtype1 objectAtIndex:1]; then iterate over that array to use the imageObjects in it.
for(ImageObject *obj in arr) {
//do stuff
}

Related

how to parse a dictionary inside an array, which is in a dictionary

In my JSON response i receive a dictionary, which has a array, inside that array is a dictionary with few key-values, to make it simple to understand here is the JSON response:
{
weather = (
{
description = haze;
icon = 50n;
id = 721;
main = Haze;
}
);
}
I want to know how can i retrieve the elements description, icon, id and main. Suppose i have a NSString called str, how can i add the value of description to this string. I have tried many different things, but none seem to work. The one i thought should work, but didn't is:
str = [[[dir valueForKey:#"weather"] objectAtIndex:0] valueForKey:#"description"];
Can someone let me know how can i access those 4 values. I don't want to create a new array of those values and then retrieve them. Regards.
Use NSJSONSerialization to convert your JSON data to NSObjects.
Then you need to index into your objects based on their structure.
If the structure is fixed, and guaranteed, you can do it with fixed indexes/keys:
//data in theJSONDict.
NSArray *outerArray = theJSONDict[#"weather"];
NSDictionary *firstWeatherItem = outerArray[0];
NSString *firstItemDesc = firstWeatherItem[#"description"];
You could also do it all in 1 line:
firstItemDesc = theJSONDict[#"weather"][0][#"description"];
But that is much harder to debug.
If this is JSON data coming from a server you need to code more defensively than above, and write code that tries to fetch keys from dictionaries, tests for nil, and either loops through the array(s) using the count of objects, or tests the length of the array before blindly indexing into it. Otherwise you'll likely crash if your input data's format doesn't match your assumptions.
(BTW, do not use valueForKey for this. That is KVO code, and while it usually works, it is not the same as using NSArray and NSDictionary methods to fetch the data, and sometimes does not work correctly.)

How to replace object index in NSMutableArray in iPhone

I have two different arrays and I want to check firstArray objects and accordingly insert objects in second array.If my firstArray contains particular object then at that index, I am trying insert value in secondArray.
Currently, I am inserting values like :
[secondArray replaceObjectAtIndex:0 withObject:transIdArray];
[secondArray replaceObjectAtIndex:1 withObject:fullCaseArray];
[secondArray replaceObjectAtIndex:2 withObject:caseTitleArray];
[secondArray replaceObjectAtIndex:3 withObject:filingDateArray];
My problem is, If in firstArray transIdArray is at 2 index then my these two arrays data getting mismatched.Please suggest me better way to check add insert values in arrays. Thanks.
NSArray elements are naturally packed together, not sparse, like C arrays can be. To accomplish what you want, the secondArray needs to carry placeholders that are considered non-objects semantically by your app. [NSNull null], an instance of NSNull (not to be confused with nil) is a common choice.
You could initialize one or both arrays like this:
for (NSInteger i=0, i<SOME_KNOW_MAX_LENGTH; ++i) {
[secondArray addObject:[NSNull null]];
}
Then an instance of NSNull in the second array means 'there's nothing at this index in this array corresponding to firstArray'. And you can check if that condition holds for a given index like this:
id secondArrayElement = secondArray[index];
if ([secondArrayElement isMemberOfClass:[NSNull class]]) { // ...
As an aside - often, when I find myself needing to coordinate parallel arrays, it usually means I have some undone representational work, and what I really need is a single array with a more thoughtful object, or the containing object must be more thoughtfully designed.

ObjectiveC-NSMutable array specific indexes stored in another array

Is there anyway I can add specific objects in NSMutableArray to another array in objective c? I can accomplish that in Java but cannot figure it our for objective c
For example I have an array of 7 strings and I only want indexes 1, 3 ,7 stored in another array.
Here is one way of creating the array from the string values at particular indexes:
NSMutableArray *array = ...; // Array with strings
NSArray *someOtherArray = #[ array[1], array[3], array[7] ];
So both array[1] and someOtherArray[0] point to the same (NSString) instance, etc.
This is what NSIndexSet (and NSMutableIndexSet) is for.
You can build it manually or use helper methods on NSArray like:
indexesOfObjectsPassingTest:
to build an index set from a block. You can then enumerate over the NSIndexSet using a for loop - using the index to call into the original array.

NSArray mutableCopy creates new array but still points to old contents

I have an NSMutableArray called playersArray in my singleton class which holds for my applications main datasource.
Each object of playersArray is a NSDictionary and the content is like :
{
sgfID = 1;
sgfPlayer = "<PlayerContact: 0xbf851b0>";
}
PlayerContact is a NSObject subclass containing properties like:
NSString * playerName, playerTeam, BOOL PlayerSelected and so on.
In one of my ViewControllers, in viewDidLoad, I want to take a deep copy of playersArray in to a NSMutableArray named duplicatePlayersArray. I do this by
playersArray = [[SGFHelper sharedHelpers] SGFContactsArray];
duplicatePlayersArray = [playersArray mutableCopy];
Now that I have two separate copies, I was under the impression that playersArray and duplicatePlayersArray are two totally different arrays in the memory. However I found that they are NOT!
Even if the debugger shows that they have different memory addresses, their contents have same memory addresses. So when i do this:
[((NSMutableDictionary *)[duplicatePlayersArray objectAtIndex:0]) setObject:#"333" forKey:#"sgfID"];
playersArray's dictionary at index:0 has ALSO "333" as key "sgfID" instead of "1" as it used to before the above line of code ran.
BUT, if I run the below code, only then, the two arrays start to differ
[duplicatePlayersArray replaceObjectAtIndex:0 withObject:tempDict];
Still this doesn't address my concern because the two arrays which I wanted to believe are different are still "connected". A change in one, results in the other array to change its contents.
Can you friends please show me a way to DEEP COPY the array I explained the contents of in a way where all of their contents are kept in different objects.
Use initWithArray:copyItems: to copy each entry in the array
NSMutableArray *duplicatePlayersArray = [[NSMutableArray alloc] initWithArray:playersArray copyItems:YES];

Saving IOS dictionary with arrays as keys

i am working with Tapku's Calendar, and i want to save some values that will be user inputed.
But i am kind of stumbled on how i would achive this, here is the layout:
// allocate the arrays and dictionary
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
NSMutableArray *dateValueArray = [[NSMutableArray alloc] init];
// set array values
[dateValueArray addObject:#"first string"];
[dateValueArray addObject:#"Second string"];
// set dictionary with date as key, and array as value
[dict setObject:dateValueArray forKey:testdate];
The dictionary dict, will be the only Dictionary, but since that dictionary uses arrays for objects, i would have multiple arrays.
So, lets say there are multiple dates registerd in "dict", different keys would have to use different arrays? Sorry i am abit confused my self here.
Is there any way i can use 1 array to store all the strings associated with different dictionary keys ?
EDIT 1
Elaboration:
The whole idea is that the user can input text that are associated with dates.
I will need to store these values and i will need to store which date they are associated to.
So i have multiple values in an array, associated with 1 date in a dictionary.
And keeping in mind that i will have to store this, i would like to know how i should assign the values to the dates.
EDIT 2:
Basically what i need for the Array is something like AddObject ForKey
Edit 3
More elaboration::
Basically i want to access the values in this manner:
[date1][note1]
[date1][note2]
[date2][note1]
[date2][note2]
And the amount of values in both date and note are variable.
If I understand what you are asking about, what you want is the property of NSDictionary allKeys which is an array of all the keys in that dictionary.
Now I see your edit. You are in the right way. To perform what you are looking for, do something like this:
First, allocate your dict somewhre:
// allocate the arrays and dictionary
NSMutableDictionary *dict = [NSMutableDictionary new];
Now, everytime you get a new date with a new string, first check if there's the first string for that date. If yes, create a new array. If not, add your string inside the previously array.
NSMutableArray *valuesForDate = dict[givenDate];
if (!valuesForDate)
{
valuesForDate [NSMutableArray new];
dict[givenDate] = valuesForDate
}
[valuesForDate addObject:#"first string for dateGiven"];
[valuesForDate addObject:#"Second string for dateGiven"];
Now you can retrieve the values with something like you wanted:
NSString *test = dict[date1][0]; //first string associated for date1
NArray *allStringsForDate2 = dict[date2]; //array with all the strings for date2
You can try using NSMapTable instead of NSDictionary, which is much more flexible but also much harder to use. You'll also find it hard to find anyone knowing the answers if you have questions about NSMapTable. But you can definitely create an NSMapTable which will use pointers of objects as keys, instead of the values.

Resources