Removing NSString form NSMutableArray - ios

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

Related

How to replace object index in NSMutableArray in iPhone

I have two different arrays and I want to check firstArray objects and accordingly insert objects in second array.If my firstArray contains particular object then at that index, I am trying insert value in secondArray.
Currently, I am inserting values like :
[secondArray replaceObjectAtIndex:0 withObject:transIdArray];
[secondArray replaceObjectAtIndex:1 withObject:fullCaseArray];
[secondArray replaceObjectAtIndex:2 withObject:caseTitleArray];
[secondArray replaceObjectAtIndex:3 withObject:filingDateArray];
My problem is, If in firstArray transIdArray is at 2 index then my these two arrays data getting mismatched.Please suggest me better way to check add insert values in arrays. Thanks.
NSArray elements are naturally packed together, not sparse, like C arrays can be. To accomplish what you want, the secondArray needs to carry placeholders that are considered non-objects semantically by your app. [NSNull null], an instance of NSNull (not to be confused with nil) is a common choice.
You could initialize one or both arrays like this:
for (NSInteger i=0, i<SOME_KNOW_MAX_LENGTH; ++i) {
[secondArray addObject:[NSNull null]];
}
Then an instance of NSNull in the second array means 'there's nothing at this index in this array corresponding to firstArray'. And you can check if that condition holds for a given index like this:
id secondArrayElement = secondArray[index];
if ([secondArrayElement isMemberOfClass:[NSNull class]]) { // ...
As an aside - often, when I find myself needing to coordinate parallel arrays, it usually means I have some undone representational work, and what I really need is a single array with a more thoughtful object, or the containing object must be more thoughtfully designed.

ObjectiveC-NSMutable array specific indexes stored in another array

Is there anyway I can add specific objects in NSMutableArray to another array in objective c? I can accomplish that in Java but cannot figure it our for objective c
For example I have an array of 7 strings and I only want indexes 1, 3 ,7 stored in another array.
Here is one way of creating the array from the string values at particular indexes:
NSMutableArray *array = ...; // Array with strings
NSArray *someOtherArray = #[ array[1], array[3], array[7] ];
So both array[1] and someOtherArray[0] point to the same (NSString) instance, etc.
This is what NSIndexSet (and NSMutableIndexSet) is for.
You can build it manually or use helper methods on NSArray like:
indexesOfObjectsPassingTest:
to build an index set from a block. You can then enumerate over the NSIndexSet using a for loop - using the index to call into the original array.

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

NSMutable dictionary not working properly

I am developing an iPad application and for this application I have one function as below :-
-(void)testcurrentest:(NSMutableDictionary *)keydictionary{
NSArray *allKeys = [keydictionary allKeys];
if ([allKeys count] > 0) {
for(int i = 0;i< allKeys.count;i++){
[_currenies removeAllObjects];
NSString *product = [NSString stringWithFormat:#"%#", [keydictionary objectForKey:allKeys[i]]];
int kl = [productPriceSeasonCode intValue];
for(int i =0;i<kl;i++){
[_currenies addObject:#"0"];
}
NSLog(#"................%#",_currenies);
[_currencydictionary1 setObject:_currenies forKey:allKeys[i]];
NSLog(#"full dictionary...%#",_currencydictionary1);
}
}
}
Here, NSLog print the currencies array based on the kl integer values but when I'm trying to set the NSMutableDictionary the currencies but mutable array always show the latest array values.
You are using the same array for all values, they should be unique objects if you don't want change of one value to affect the other values. Initialise _currenies on every loop step or use its deep copy when preparing a new object.
A bit of code:
[_currenies removeAllObjects]; // < The same array you've added to dict on previous loop steps
Creating a new array at each loop step would create a unique object for all key-value pair:
_currenies = [NSMutableArray array]; // < Note it is not retained, apply memory management depending on your project configuration
Your code is a garbled mess. As others have pointed out, you are using the same loop index, i, in 2 nested loops, making it very hard to tell your intent. Don't do that, ever. It's horrible programming style.
You are also creating a string "product" that you never use, and fetching the same integer value of productPriceSeasonCode on every pass through the outer loop. I suspect you meant to fetch a value that varies with each entry in your keydictionary.
Then, you have an array, _currenies, which you empty on each pass through your outer loop. You then add a number of "0" strings to it, set a key/value pair in your _currencydictionary1 dictionary to the contents of that array, and then repeat. Since you re-use your _currenies array each time, every key/value pair you create in your _currencydictionary1 dictionary points to the exact same array, which you keep changing. At the last iteration of your outer loop, all the entries in your _currencydictionary1 will point to your _currenies array, which will contain the last set of contents you put there.
Create a new array for each pass through your outer array, and add that newly created array to your _currencydictionary1. You want a unique array in each key/value pair of your _currencydictionary1.
In short, NSMutableDictionary is working just fine. It's your code that isn't working properly.
Not an answer but comments don't have formatting.
The question should provide more information on the input and desired output.
First simplify your code and it should be easier to find the error:
-(void)testcurrentest:(NSMutableDictionary *)keydictionary{
NSArray *allKeys = [keydictionary allKeys];
for(NSString *key in allKeys) {
[_currenies removeAllObjects];
int kl = [productPriceSeasonCode intValue];
for(int i =0; i<kl; i++){
[_currenies addObject:#"0"];
}
NSLog(#"................%#",_currenies);
_currencydictionary1[key] = _currenies;
NSLog(#"full dictionary...%#",_currencydictionary1);
}
}
Note: product was never used.

Exception in button tag

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.

Resources