changing the value of key in an array of dict - ios

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.

Related

Multi Dimensional NSArray to NSArray?

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.

Clear arrays inside NSDictionary

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

Unable to remove all custom objects from array in Objective-C?

I have array of custom objects. I am removing objects from that array but I am not able to remove the objects from that array here is my code.
if([arr_userFav count]>0)
{
int i;
for (i=0; i<[arr_userFav count]; i++)
{
Post *objectCopy = [arr_userFav copy]; //create a copy of our object
[arr_temp_Fav addObject: objectCopy];
}
NSLog(#"size of arr_temp_Fav %lu",(unsigned long)[arr_temp_Fav count]);
[arr_userFav removeAllObjects];
}
after removing objects form arr_userFav I have size of array as zero but when I put a breakpoint after it & see the objects in it then I don't get a empty array. Please tell how to do it?
Below is the screenshot
Please look very carefully at what your code is doing.
The object that you add to arr_temp_fav is a copy of arr_user_fav, that is a copy of the complete array. So if your array originally had ten elements, you now have an array with ten arrays as elements, each with ten elements itself. If you had 10,000 elements it's a lot worse.
Post *objectCopy = [arr_userFav copy]; //create a copy of our object
Here we have a case of a comment that would be completely pointless if it was correct - it is however incorrect and may be hiding from you what you are doing wrong.

NSMutableArray Allocate then replaceObjectAtIndex

I have a NSMutableArray that i define in the header file as:
#property (strong, nonatomic) NSMutableArray *tempPhotosArray;
Then i allocate as:
_tempPhotosArray = [[NSMutableArray alloc] init];
What i'd like to know is if i then go to replaceObjectAtIndex the program will complain on an out of bounds. I want to keep only a set number of items in that array, so is it possible to do a insert or replace? i.e. if at index 0 it is empty do an insert, if there is an object already replace it?
Thanks
i think i agree with Hani Ibrahim. Since you said you only want to keep a set number of objects in the array. So how many you want?
// add these code when you initialize the array
int aSetNumber = 5;
_tempPhotosArray = [[NSMutableArray alloc] init];
for (int i = 0; i < aSetNumber; i++)
{
[_tempPhotosArray addobject: [NSNull null]];
}
i guess then you can do whatever you want, i don't know what exactly you want to do in this case, but i would check if the object in that position is NSNUll, if so, replace that, if not, i don't know what you want them
//use these code when you trying to insert the real object
if([[_tempPhotoArray objectAtIndex:anIndex] isKindOfClass: [NSNull class]])
{
//replace it here
}
As to why you are getting an error, what everyone else wrote is accurate, but....
The description of what you want doesn't match what an NSArray is. It sounds like you want a list of up to 5 items and never more than 5. It might be that if you try to add a 6th item the "oldest" goes away. Like a "recently opened" file history. You can make this type of functionality with an NSArray, but that's not what it is out of the box.
I would suggest making your own object class. I'm not going to write all the code for you, because this sounds suspiciously like programming homework, but I will point you in the correct direction.
FivePack <-- our class
NSArray *storage; <-- where we house the data
// a public method which lets you add things.
- (void)addItem:(id)item {
int indexOfLastItemInArrayToSave = 4;
if (storage.length < 4)
indexOfLastItemInArrayToSave = length-1;
NSRange range = NSMakeRange(0, indexOfLastItemInArrayToSave);
NSArray *temp = [storage subArrayWithRange:range];
// now create a new array with the first item being "item" that
// was passed in and the rest of the array being the contents of temp.
// Then save that to storage.
}
What you want to do with the data and writing something to get it from your new object is up to you, because I'm not sure how you want to do it.
There are no objects in the array when you initially created it, so there is nothing to replace.
Like this?
if([_tempPhotosArray count] > 0)
//replace object
else
//add object to array

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).

Resources