Multi Dimensional NSArray to NSArray? - ios

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.

Related

Adding array of dictionary to array of dictionary

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];
}

update value at specific index in NSArray

I am hoping not to need to use an NSMutableArray here. I have an array with 10 elements. I want to change the value at index 4. Is there a way to do this without having to use NSMutableArray? The problem with NSMutableArray is that we can change anything about it, including its size. I don't want the size of this array to change accidentally. I just want to change the value at index 4 from say 22 to 25. How might I do that? doing array[4]=25 is not working.
NSArray *ar1 = #[#"1",#"2"];
NSMutableArray *ar1update = [ar1 mutableCopy];
ar1update[1] = #"Changed";
ar1 = [NSArray arrayWithArray:ar1update];
The only way is to create a new NSArray and change your pointer to a new NSArray. I can give an example...
In interface:
#property (strong, nonatomic) NSArray *myArray;
In implementation:
- (void) updateMyArray{
NSMutableArray *myArrayMut = [self.myArray mutableCopy];
myArrayMut[4] = #"new item";
self.myArray = [myArrayMut copy];
}
So basically, you can create a mutable copy temporarily, make the change you need, and then make an immutable copy. Once you have the immutable copy, you can point myArray to the new copy. As long as you are only changing existing items in updateMyArray and the myArray starts out with 10 items or less, you will never be able to have more than 10 items.
If you don't wish to use NSMutableArray how about a plain old C array? E.g.:
int array[10];
...
array[4] = 25;
You can store Objective-C objects in such an array and ARC will handle the memory management.
If you really want a fixed-sized NSArray/NSMutableArray you can do that by subclassing those types yourself - subclassing NSArray only requires implementing two methods and you can use an underlying C array or NSMutableArray for the actual storage.
HTH

changing the value of key in an array of dict

I have an array that has different data, one being temperature recorded in celcius. when a user changes the default setting to fahrenheit i need to change the array of information when it displays in a collectionView. the program compiles properly, but when the view loads the compiler crashes with no error and just highlights the line in green and say in right corner Thread 1: breakpoint1.1
The line highlighted is bellow
[dict setObject:[NSNumber numberWithInteger:number] forKey:#"temp"];
i have also just tried with no success just to bypass the problem:
[dict setObject:#"11" forKey:#"temp"];
my code for looping through the array and changing the data is:
changedArray = [[NSMutableArray alloc] initWithCapacity:50];
for (NSMutableDictionary *dict in locationArray) {
if ( [[dict objectForKey:#"site"] isEqual:[defaults objectForKey:#"location"]] ) {
NSInteger number = [[dict objectForKey:#"temp"] integerValue];
number = ((number * 1.8) + 32);
[dict setObject:[NSNumber numberWithInteger:number] forKey:#"temp"];
[changedArray addObject:dict];
}
}
If i remove the three lines of code changing the temperature value in the dict, it compiles and run correctly. Any inside for this ios noob would be great thanks. :-)
I guess the dictionary instances in your array are all immutable NSDictionary object. Just type casting won't turn them to mutable ones. Try the following
Iterate through Array
Find the correct dictionary
Form a mutable NSMutableDictionary
Edit the mutable one
Update back to the array (The array also needs to be a mutable one)
But my advice would be to keep the temperate in only one unit always, the unit which the users tend to use more. Only when you need to display it, check with the user chosen unit do the calculation and refresh it.

What's the Best Practice for Implementing Multi-section TableView

All,
I have about 3000 words with definitions that I am loading into a TableView. Right now, it's just a sorted list of words, sans the sections because I haven't added them yet.
I need to add sections to my TableView data (A,B,C ...) and there seems to be several ways to do this so before I jump into this I am looking for some confirmation or correction if I am going down the wrong rabbit hole.
Currently the data that the TableView reads is stored as objects in an NSMutableArray per this code:
//AppDelegate.m
- (void)applicationDidFinishLaunching:(UIApplication *)application
{
//...
NSMutableArray *wordArray = [[NSMutableArray alloc] init];
//Loop through result set from DB and populate objects
while([rs next]){
[wordArray addObject:[Word wordWith:[rs stringForColumn:#"word"]
Definition:[rs stringForColumn:#"definition"]
SectionIndex:[rs stringForColumn:#"sectionIndex"]]];
}
MainViewController *mainViewController =
[[MainViewController alloc] initWithNibName:#"MainView" bundle:nil];
mainViewController.listContent = wordArray;
//...
}
Each object has a section index value ([A-Z0-9]) so I already know which section each word goes in, I know what the sections need to be and I can easily derive a count of objects for each section. All the words have been sorted via SQL before the NSMutableArray was populated so that's already handled.
Can I create multiple sections with the one NSMutableArray or do I need to do something different?
Thanks
You could store your words into arrays inside a NSDictionary holding keys for each letter.
Number of sections would return
[[dictionary allKeys] count];
Title for section
NSArray * keys = [dictionary allKeys];
[keys objectAtIndex:sectionIdx]
Number of rows in section would return
NSArray * keys = [dictionary allKeys];
[(NSArray *)[dictionary objectForKey:[keys objectAtIndex:sectionIdx]] count];
Each word would be
NSArray * keys = [dictionary allKeys];
[(NSArray *)[dictionary objectForKey:[keys objectAtIndex:sectionIdx]] objectAtIndex:indexPath.row];
I have found that you sometimes want to add sorting to your lists and then, another approach might be interesting. Put all your models (Word's in your example) in a dictionary with some unique value of the model as the key.
Implement a sorting method, that you run every time the underlying dictionary changes. The sorting method will use e.g. keysSortedByValueUsingComparator on the dictionary and supply a different blocks for different sort orders. Let the sorting method create section arrays and add keys in the arrays that corresponds to the keys in the dictionary.
You do not store anything twice and you get different sort orders by just providing different sort blocks (that can look at any properties of your model class).

How to access specific column in NSArray using Xcode

I'm new to iOS programming. I'm trying to bind the specific field from the objects in an array to a UITableView. Here's my code:
NSArray *personInfo; //contains a record with fields: name, address, email
personInfo = [[PersonDatabase database] getAllPersons]; //pulling the record into array
From there, I'm trying to get the field "name" from my array.
cell.textLabel.text = [NSString stringWithFormat:#"%#", [personInfo objectAtIndex: indexPath.row] retain]
As it seems you have objects in your array, what you may be looking for is the -[NSArray valueForKey:] method (documentation here).
For example:
NSArray *names = [personInfo valueForKey:#"name"];
This should return you an array containing all of the names in the array.
Are you trying to create a 2D Array?. If so you'll need to call objectAtIndex: twice on it in a nested call, but since you're new I'd suggest breaking down to a few lines so you can see more clearly what is happening.
Also, theres heaps of good code snippets on google for dealing with NSArray and table view.
Please check that if you delcare your array in #interface file as
//////.h
NSArray *personInfo;
#property(nonatomic ,retain) NSArray personInfo;
and then
in #implementation file
add this line
#synthesize personInfo;
hope it works

Resources