I had two NSDictionary elements in the finalOrderArray before addingObject. Then I added sharedData.comboItems but this object is also an array of NSDictionary.
Now,I have a mix of NSDictionary and NSArray which is difficult to handle.
Is there an easy way to add NSDictionary all together?
[finalOrderArray addObject:sharedData.comboItems];
Desired output in this example, finalOrderArray would have 6 dictionaries rather than having 2 dictionaries and one array of dictionary.
Use addObjectsFromArray: method.
Adds the objects contained in another given array to the end of the
receiving array’s content.
Here is the link to NSMutableArray and all its methods.
Use addObjectsFromArray method. It will add all the objects from sharedData.comboItems. Try this.
[finalOrderArray addObjectsFromArray: sharedData.comboItems];
sharedData.comboItems is an array that's why you get one array in your finalOrderArray. You need to iterate through the array, get the dictionary and add to finalOrderArray.
Like this:
for (NSDictionary *item in sharedData.comboItems) {
[finalOrderArray addObject:item];
}
Related
Firstly refer to the graphic show below:
I have an NSArray that contains 9 Elements.
Each of these 9 elements contain a further 5 Elements.
What I would like to do is take the [*][1] from each an place them into another which will only contain these dates.
How is this best achieved ??
NSMutableArray * results = [#[] mutableCopy];
for (NSArray *details in self.fuelDetailsForSelectedBike){
[result addObject:details[1]];
}
This seems rather basic, loop through the main array and for each object (that is an array) take it's second element and add it to the result.
I want to implement "expanded" behaviour on click on table view headers. For that, i have NSDictionary, which is have all data in form key -> array of values.
What i want is, create other dictionary, copy of initial, and remove all data in arrays inside it. So, in initial loading, our table will look like "closed" headers, after tap on each one, it will collaps and show values corresponding to given key. After tap on header aggain, it will "close" and hide values.
So, basically i want to:
1) enumerate through an NSDictionary and remove all data from array (or create new empty arrays)
2) dynamically add/remove data for given key
Is there easy way to achieve that?
How about this:
NSMutableDictionary *newDict = [NSMutableDictionary new];
for id aKey in tableDict {
newDict[aKey] = [NSMutableArray new];
}
tableDict = newDict;
[tableView reloadData];
Edit:
To clear a single key
tableDict[specificKey] = [NSMutableArray new];
To copy the array from one key into another:
tableDict[specificKey] = [((NSMutableArray *)tableDict[otherKey]) mutableCopy];
I have my own class with several properties and I have them in NSArray. I need to use them for method which takes NSArray of strings. So I am asking what is best aproach to get array with strings from my array which has custom classes. I can create second array and use it but I think there could be better way. I need to have it for different custom classes (from one, I want to use for example name property to new NSArray, in second title property and so).
I hope I explained well but I tried it once more on example:
NSArray *arrayWitCustomClasses = ... fill with custom classes;
// setValues method takes NSArray with NSStrings
// when arrayWithCustomClasses used it returns copyWithZone: error on custom class
[someObject setValues:[arrayWithCustomClasses toArrayWithStrings]];
As long as your object exposes the required values as NSString properties you can use the valueForKey method of NSArray.
For example
NSArray *arrayOfTitles=[arrayWithCustomClasses valueForKey:#"title"];
NSArray *arrayOfNames=[arrayWithCustomClasses valueForKey:#"name"];
Or
[someObject setValues:[arrayWithCustomClasses valueForKey:#"title"]];
and so on
NSMutableArray *strings = [NSMutableArray array];
for (NSObject *item in arrayWithCustomClasses) {
/* You can use a different property as well. */
[strings addObject:item.description];
}
[someObject setValues:strings.copy];
Like #Tim says, but you could shorten it by just using:
[someObject setValues:[arrayWithCustomClasses valueForKey:#"description"]];
Same result. One line of code.
Then implement the description method of your custom classes to return whatever properties and formatting you want.
It is a simple pull to refresh case. I have data loaded into table and have a mutable data array at back-end, I receive a array of new data and want to add this complete array at start of existing array.
One workaround is to create new array with new arrived data and then add previous array into it using addObjectsFromArray: method. Is there some workaround to add new data array to the start of previous array directly?
First, build an NSIndexSet.
NSIndexSet *indexes = [NSIndexSet indexSetWithIndexesInRange:
NSMakeRange(0,[newArray count])];
Now, make use of NSMutableArray's insertObjects:atIndexes:.
[oldArray insertObjects:newArray atIndexes:indexes];
Alternatively, there's this approach:
oldArray = [[newArray arrayByAddingObjectsFromArray:oldArray] mutableCopy];
NSMutableArray offers the insertObjects:atIndexes: method, but it's easier to append the way you suggest using addObjectsFromArray:.
-insertObject:atIndexes: is easy enough, and should (I believe) be more efficient than using -addObjects and swapping arrays. It'd probably end up looking something like this:
[existingResults addObjects:newResults atIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, newResults.count)]]`
Creating a new array is probably your best solution, but you can also use a loop
NSUInteger index;
index = 0;
for ( id item in sourceArray )
{
[destArray insertObject:item atIndex:index];
index++;
}
Just simple way:
NSMutableArray *arrayTmp = [firstArr addObjectsFromArray:myArray];
myArray = arrayTmp;
Hi what I want to do is search an NSMutable array for the string str or the header label in the view. If the word already exists in the array then I want to delete it. Otherwise I want to add it. Here's my code so far.
-(IBAction)add:(id)sender{
NSMutableArray *array=[[NSUserDefaults standardUserDefaults] mutableArrayValueForKey:#"favorites"];
NSString *str=header.text;
}
See the manual: NSArray Class Reference