NSMutable array exception - ios

I am getting data from web service and displayed it in a tableview and at button click which is at tableview i am storing data id in nsmutablearray . Then i am comparing clicked data id which is stored in nsmutable array with web service data in cellForRowAtIndexPath function.
Error:
At code comparing in tableview, exception index not found.
At button click i tried these all::::
[myidmutablearray addObject:[[webservicedata valueForKey:#"id"]objectAtIndex:clickedButtonIndexPath.row]];
[myidmutablearray insertObject:[webservicedata valueForKey:#"id"] atIndex:clickedButtonIndexPath.row];
[myidmutablearray replaceObjectAtIndex:clickedButtonIndexPath.row withObject:[webservicedata valueForKey:#"id"]];
At CellForRowAtIndexPath I tried this,
if([myidmutablearray count]!=0)
{
if([[webservicedata valueForKey:#"id"] containsObject:[myidmutablearray objectAtIndex:indexPath.row]])
NSLog(#"not empty");
}
else
NSLog(#"empty");

NSMutableArray is not an array with an indefinite number of entries, whose index you can assign.
As you add objects to an NSMutableArray, the first entry added will be at index 0. No matter what index you give to it when adding.
I think What you really want is an NSDictionary, with the key as an NSNumber made from the indexpath.row. Then you can retrieve it later using an NSNumber again.
When saving, do something like this:
//defining somewhere
NSMutableDictionary *myIdMutableDictionary;
//then in the button click:
[myIdMutableDictionary setObject:[webservicedata valueForKey:#"id"] forKey:#(indexPath.row)];
//then later to check it, you test value would be in this:
[[myIdMutableDictionary objectForKey:#(indexPath.row)

Related

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

I need an NSMutableDictionary, where the values will be arrays, that will be created while the app is running, and filled up later

So at runtime I initialize my NSMutableDictionary, and then when the user clicks on certain buttons, the app needs to get the name of this button and this name will be the key. Then he will click on another button, and this button's name will be put into an array, and the array will become the value of the first button name's key. However, later the user will keep doing this, and if he ends up clicking on a button that already is a value in the dictionary, then the second button he presses must be added to the array already associated with that key. Here is my code so far.
closestBeaconsDictionary is the main dictionary. closestBeaconName is a variable that makes up the keys, and pinNumberName is the value that must be put into an array. closestBeaconName and pinNumberName are constantly changing based on what buttons the user presses.
//first, check to see if the key already exists. If not, then add the key to
//closestBeaconsDictionary and then create an array and add this array to the dictionary
//and then add pinNumberName to this array.
if ([closestBeaconsDictionary objectForKey:pinNumberName] == nil)
{
NSMutableArray *closestPinsToBeacon = [[NSMutableArray alloc] init];
[closestPinsToBeacon addObject:pinNumberName];
[closestBeaconsDictionary setObject:closestPinsToBeacon forKey:closestBeaconName];
}
else
{
//what do I do here?? How do I access the dictionary that is at key:pinNumberName
//and then add an object to it?
}
So, my question is basically what do I put in the else statement? I'm confused because isn't closestPinsToBeacon basically destroyed after the block of code runs?
Try this:
if ([closestBeaconsDictionary objectForKey:pinNumberName] == nil)
{
NSMutableArray *closestPinsToBeacon = [[NSMutableArray alloc] init];
[closestPinsToBeacon addObject:pinNumberName];
[closestBeaconsDictionary setObject:closestPinsToBeacon forKey:closestBeaconName];
}
else
{
NSMutableArray *arr = [[NSMutableArray alloc] init];
arr = [[closestBeaconsDictionary objectForKey: closestBeaconName] mutableCopy];
[arr addObject:pinNumberName];
[closestBeaconsDictionary removeObjectForKey: closestBeaconName];
[closestBeaconsDictionary setObject:arr forKey: closestBeaconName];
}
Hope this helps.. :)

Finding the equivalent index of a chosen row in a filtered NSMutableArray (from searchResultsTableView) in unfiltered NSMutableArray

Apologies in advance if my question is unclear as I'm new to iOS programming (and programming in general).
Right now, I have a UITableView that displays an alphabetical NSMutableArray of the names of a user's friends, something like:
friends: { #"Andy",#"Anand",#"Bob",#"Cat" };
I have another NSMutableArray that holds the matching ages of the user's friends, something that the user never sees but is passed on to the previous view controller after the user picks the name of a friend. This ages array is something like:
friendsAges: {#"25",#"23",#"30",#"22"};
My question is, if I added a searchBar to the UITableview of the friends' names, how do I make sure that the corresponding age object is sent back to the previous view controller if the user picks a name from the filtered array, searchResultsTableView?
For example, if there wasn't a searchBar, picking "Bob" would also send "30" to the previous controller. But if the user types in "bo" and only Bob is shown in the filtered searchResultsTableView, then age "25" will be sent instead to the previous controller b/c it's the first index.
Here's my incorrect code:
if (tableView == self.searchDisplayController.searchResultsTableView) {
NSString *theFriend = [_filteredFriendArray objectAtIndex:indexPath.row];
// not sure what I should use instead for the _filteredFriendArray below
NSString *theFriendAge = [_filteredFriendArray objectAtIndex:indexPath.row];
[self.delegate friendPickerViewController:self
didSelectFriend:theFriend
didSelectFriendAge:theFriendAge];
} else { //if not using search bar and seeing filtered search results
NSString *theFriend = [friends objectAtIndex:indexPath.row];
NSString *theFriendAge = [friendsAge objectAtIndex:indexPath.row];
[self.delegate friendPickerViewController:self
didSelectFriend:theFriend
didSelectFriendAge:theFriendAge];
}
I feel like it might be easier to use an NSDictionary instead of trying to match the index of a filtered search view back to main array, but am not sure how to best do that.
Any help would be appreciated!
You are right, it is easier to use NSDictionary. Each person can be represented by an NSDictionary like #{#"name":#"Stephen Hawking", #"age":70}. Populate your friends table view with [personDict objectForKey:#"name"] and on selection get the value for [person objectForKey:#"age"] and pass it back to the previous view controller.

how do i select specific items from a array to display in UITableView?

I am new in iOS programming, and i an writing my first app. I have a NSMutableArray with items in it.These objects has title, id etc. and favorite attribute. The favorite attribute is a bool and tells if the item is added to favorites by the user. Now to my question: in UITableView, i only want all the items with favorite = YES to be displayed in the 'table'. How do i do that ? Do i have to loop through the array and save these items in a new array before i come to the method cellForRowAtIndexPath ? Like in viewDidLoad maybe? Because i tried to set a condition in the method cellForRowAtIndexPath method, but then there was only a bunch of empty cells displayed + my one favorite item. Help Pleese !
I would loop with a for-in statement through the array and make a new *favoriteArray with all the items in it, that you want displayed in the tableView.
So something like:
NSMutableArray *favoriteArray = [NSMutableArray new];
for (Item *item in self.mutableArray) {
if (item.favorite) {
[favoriteArray addObject:item];
}
}

Search NSMutableArray

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

Resources