Exception in button tag - ios

I have set the tags for the buttons but in this method i am getting an exception and i am not sure why
- (IBAction)showComments:(UIButton *)sender
{
int tag=[sender tag];
NSLog(#"The tag clicked:%#",[blogids objectAtIndex:tag]);
}
Where blogids is my NSMutableArray
Thanks

You are getting NSRangeException, which means you are trying to retrieve that element of array which is not existing. I suggest you should check the array count with Tag value which you are trying retrieve.
NSLog(#"%d",[blogids count]);
NSLog(#"%#",tag);
I am sure tag value is greater than count. That should not be, if you want to retrieve value from array using tag.
Thanks,

The reason you are getting an exception is because your tag is greater then the blogids count.
Add the buttons to the array and then it will not crash.
For example:
blogids = [[NSMutableArray alloc]init];
[blogids addObject:oneOfYourButtons];
Also if you only want to see the tags number use this:
NSLog(#"The tag clicked:%d",tag);
instead of:
NSLog(#"The tag clicked:%#",[blogids objectAtIndex:tag]);

Your blogids array is blank. Please check if it has object in the index you got from button tag

Your blogids is empty array here. so that, it show as bounds as [0 .. 0](that is array count is zero). Just check your array initialization.

Related

How to add object / value to a certain element on NSMutableArray?

I only know method addObject, which is add object to the next element in the array. I want to be able to add / set / update object to an arbitrary position at the NSMutableArray, ex:
arr[105] = #(true);
arr[709] = #(30);
arr[1010] = #"Hello world!";
NSLog (#"%#", arr[1010]);
I have been trying something like this, but the next time I tried to retrieve the value, it says nil. How to do this? Thanks.
EDIT: last time I tried, it gave me error: index 1010 beyond bounds for empty array.
you specify a size when you create an array, the specified size is regarded as a “hint”; the actual size of the array is still 0. This means that you cannot insert an object at an index greater than the current count of an array. For example, if an array contains two objects, its size is 2, so you can add objects at indices 0, 1, or 2. Index 3 is illegal and out of bounds.
read more at https://developer.apple.com/library/prerelease/ios/documentation/Cocoa/Reference/Foundation/Classes/NSMutableArray_Class/index.html
below method use for replace existing object at index with new object
[self.arr replaceObjectAtIndex:<#(NSUInteger)#> withObject:<#(nonnull id)#>]
below method use for insert new object at index
[self.arr insertObject:<#(nonnull id)#> atIndex:<#(NSUInteger)#>]
Ex:
if object already available at index and replace with new object
[self.arrayBuyers replaceObjectAtIndex:1010 withObject:#"Hello world!"]
add new object at index
[self.arr insertObject:1 atIndex:#"Hi"];

Unable to store sqlite3_last_insert_rowid into an array

I am using the following code to store "sqlite3_last_insert_rowid" into NSMutableArray,I am getting the rowid but nothing is stored in the array. It is giving me null.
NSUInteger rowIDNum=sqlite3_last_insert_rowid(myDatabase);
NSNumber *xWrapped = [NSNumber numberWithInt:rowIDNum];
[_rowID insertObject:xWrapped atIndex:0];
NSLog(#"row ID array %#",[_rowID objectAtIndex:0]);
Could you please tell me the correct way to store rowid into an array?
I suspect the array is not allocated, else you would get an NSRangeException if the array was allocated but empty (i.e. the first time you called that method).
From the NSMutableArray reference:
index
The index in the array at which to insert anObject. This value
must not be greater than the count of elements in the array.
Important: Raises an NSRangeException if index is greater than the
number of elements in the array.
You would normally allocate the array in the class init method or viewDidLoad method, depending on what the class is. Once you've allocated the array, use:
NSUInteger rowIDNum=sqlite3_last_insert_rowid(myDatabase);
[_rowID insertObject:#(rowIDNum)];

cannot insert into NSMutableArray due to indexvalue being out of bounds

I have a MutableArray on this view called array and the object in question is detailItem, which has a property of rank (int). On this view, there's a text field displaying the rank and I want to be able to move the detailItem up and down the MutableArray by changing the rank.
So, for example let's say the detailItem has a rank of 3, which is index value of 2. If I change this in the text field to 3, I want the array to adjust and move it down one place. However, as I type in the value of rankField (the text field), it crashes the app since it automatically updates the value before I'm done editing. So, if I click on the text field and write 23 (planing on deleting the 2) or just press delete (now the value is nil) the app crashes with an uncaught exception.
Here's the code:
- (IBAction)rankFIeldTextChanged:(id)sender {
QueueMember *member = self.detailItem;
[self.array removeObjectAtIndex:self.detailItem.rank];
if (0<= [self.rankField.text intValue]<= self.array.count) {
[self.array insertObject:member atIndex:[self.rankField.text intValue]-1];
}
}
The if condition of making the text value in-between the array size and 0 seems to have no effect.
btw this is all in the detailsViewController which is connected to the main view controller via push segue. does it make more sense(or more better coding) to just set the new rank value in details and actually make the array changes in the mainviewcontroller.m?
The problem is that you are trying to do two boolean statements at once (which doesn't work). Change your if statement to something like:
if (0< [self.rankField.text intValue] && [self.rankField.text intValue] < self.array.count) {
//Insert your object here
}
else
{
//Add object here
}
Your current setup check to see if 0<= [self.rankField.text intValue], which will return true for all values greater than or equal to 0. Then it checks the result of that (YES:1, NO:0) if it's less than or equal to your array count. That will always return true if your array has anything in it. So basically your check will always return true.
Since it always returns true I could check for array object number 1000, your if statement says go for it, then I check and the array says "No way in heck!" and crashes your app.
EDIT: Updated my code snippet to take into account your array insertion line.
I'd just do this
- (IBAction)rankFIeldTextChanged:(id)sender {
QueueMember *member = self.detailItem;
[self.array removeObjectAtIndex:self.detailItem.rank];
if ((0<= [self.rankField.text intValue])&&([self.rankField.text intValue]<= self.array.count)) {
if (self.array.count >([self.rankField.text intValue]-1)){
[self.array insertObject:member atIndex:[self.rankField.text intValue]-1];
}
}
}
you are probably trying to insert at an index greater than the count of the array.

Removing NSString form NSMutableArray

I have an array with 3 strings that all equal "Farm"
When I try to remove ONLY ONE "Farm" with this line of code:
[array removeObject:#"Farm"];
It removes all.
How can I only remove one?
First you just need to get the index of one of the "farm" strings. If index is found, you can then remove the object at that index from your array.
NSUInteger index = [array indexOfObject:#"farm"];
if (index!=NSNotFound) {
[array removeObjectAtIndex:index];
}
removeObject means removes all occurrences in the array of a given object.
See the description :
This method uses indexOfObject: to locate matches and then removes them by using removeObjectAtIndex:.
So , use removeObjectAtIndex , or maybe removeLastObject.
Try something like this...
[array removeObjectAtIndex:<index of object to delete>];

why is it not assigning the string to the label? or how do i get the value out of the array into the label?

I'm trying to assign a label a value from an Array but I don't know why its not working
im trying to add the value of cowid from the array info into the label vaca, but its not working.
cowid is clearly null here so has not been initialized. stringWithFormat shows this with the (null). You can have the confirmation by
if (detalles.Cowid == nil) {
NSLog(#"The Cow has no id");
}
I found the error and the problem was that i wasn't populating the array before i used it to it was empty. i Solved it by simply putting DetDiagnostico *detalles = [info objectAtIndex:0]; after self.info = objects; this worked just fine!

Resources