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.
Related
I have an NSArray mentioned below.
a{
some objects
}
b{
some objects
}
c{
some objects
}
I would like to know if there is any way to store a,b index into one index in another NSArray ?
First convert your NSArray to NSMutableArray in which you wants to add element and then
Put both object form other array In a NSMutableDictionary for particular keys and then add NSMutableDictionary to object desired index in NSMutableArray.
You need to find these two object add them to the new NSArray(it will have 2 objects inside) and add this array to an another array at index 0 as you want
I have 2 array of strings, lets say for simplicity it will be Array 1 - #[#"a",#"b",#"c"] and Array 2 will be - #[#"b",#"c", #"d"].
What i want is, to create an array from strings (yes, there will be strings, sometimes long enough, i put characters just for simplicity) that exclude strings contained in previous array. And second array, that otherwise, contain string that exist in first array and not exist in second.
So, with array i privded, it would be :
resultArray1 = #[#"d] (exist in second array, but not in first)
resultArray2 = #[#"a"] (exist in first array, but not in second)
How to enumerate through this arrays to get what i want to? Thanks.
Make a NSMutableArray from the array with the items you want to keep, and call removeObjectsInArray: on it:
NSMutableArray res1 = [arr1 mutableCopy];
[res1 removeObjectsInArray:arr2];
NSMutableArray res2 = [arr2 mutableCopy];
[res2 removeObjectsInArray:arr1];
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.
I am trying to add datas to a dictionary using key value array but the dictionary values are arranged based on keys alphabetical order
my values and keys array contains :
myValueArray ={[orange,apple],
[ford,toyota,lexus]}
myKeyArray ={[fruits],[cars]}
I am adding keys and value to a NSMutabledictionary using
myDictionary = [[NSMutableDictionary alloc]initWithObjects:myValueArray forKeys:MainDelegate.myKeyArray];
myDictionary should be
myDictionary = { Fruits =(orange,apple);
cars =(ford,toyota,lexus);}
but it is added in key's alphabetical order and looks like
myDictionary = { cars =(ford,toyota,lexus);
Fruits =(orange,apple);}
is there a way to add key value pairs in dictionary as it is without any ordering.
I am trying this to populate a grouped uitableview. since keys are used as sections everything looks like contacts app where sections and its corresponding values are arranged alphabetically. But i need the recent section to be shown first(the first one in array).
If you need to conserve your initial order, then you have to use an NSMutableArray.
NSMutableDictionary is designed to be able to access the values for key in the fastest possible way, and having the key sorted helps with that.
Here is how to declare the dictionary and array and how to use them for a grouped tableView:
NSDictionary *myDictionary = #{ #"cars": #[#"ford" , #"toyota", #"lexus"], #"Fruits": #[#"orange", #"apple"]};
NSArray *keysArray = #[ #"Fruits", #"cars"];
// first section
NSString *sectionName = keysArray[0];
NSArray *sectionArray = myDictionary[sectionName];
// second section
sectionName = keysArray[1];
sectionArray = myDictionary[sectionName];
NSMutableDictionary doesn't have any sorting associated to it, when you print this in log, it will display in sorting order.
You can manage a separate NSMutableArray and put you key in the order in which you want to fetch from NSMutableDictionary.
That can be the easiest way to store values and fetch as per your requirement.
May this help you.
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.