Remove item at index - iphone

In my iCarousel, I use this to remove the items at index:
NSInteger index2 = carousel2.currentItemIndex;
[carousel2 removeItemAtIndex:index2 animated:YES];
[items2 removeObjectAtIndex:index2];
So the image in the view of the specific index is remove.
But I don't know how to reduce the count in my array to 1 or reduce the index count. How to do it?
sorry for the bad english.

When you remove any object from NSMutableArray the length of array automatically reduced by 1. You can check length of array by using
[carousel2 count];
and
[items2 count];

I did not get so much idea from your question. but I think you have to reload iCarousel after remove item.

Related

how do I get the index at which an Object has been added to an NSMutableArray?

I'm adding an object to a NSMutableArray like this:
[array addObject:myObject];
I now want to send a reference to my delegates of the Array Index where this object was added.
Is there an easy way to find out the index where my object was added in the array so that later I can call
[array objectAtIndex:index]
to get a reference back for it?
Thanks!
Rather than passing the index of an object (which could be incorrect) to your delegate, pass a reference to the object itself. If the delegate needs the index of the object in the array, it can figure it out itself using -indexOfObject: as Antonio MG describes. The index of any given object in a mutable array can change as objects are added, inserted, and deleted. Counting on indices to remain valid over any period of time is like leaving a jelly sandwich on the counter -- it's sure to attract bugs.
You need to serialize access to a mutable array and -addObject: always adds the object to the end of the array. Given those two assertions, you know the index of the next added object will always be the current length of the array. So the following will hold true:
NSUInteger nextIndex = [array count];
[array addObject:myObject];
// you can now tell your delegates that nextIndex is the index of myObject
Use this method for that:
index = [animalOptions indexOfObject:myObject];
The latest added object should be at [array count] - 1 index. You can always rely on "count - 1" scheme to determine the last index.
If you call addObject you always add the object at the end (so count - 1).
You can use "insertObject:atIndex:" to specify an index.
For your question: indexOfObject:
Direct after adding the object's index is array.count -1 .

Dynamically create arrays at runtime and add to another array

I am working on a small isometric engine for my next iPhone game. To store the map cells or tiles I need a 2 dimensionel array. Right now I am faking it with a 1-dimensionel, and it is not good enough anymore.
So from what I have found out looking around the net is that in objective-c I need to make an array of arrays.
So here is my question: How do I dynamicly create arrays at runtime based on how many map-rows I need?
The first array is easy enough:
NSMutableArray *OuterArray = [NSMutableArray arrayWithCapacity:mapSize];
now I have the first array that should contain an array for each row needed.
Problem is, it can be 10 but it can also be 200 or even more. So I dont want to manually create each array and then add it. I am thinking there must be a way to create all these arrays at runtime based on input, such as the chosen mapsize.
Hope you can help me
Thanks in advance
Peter
I think this previous question should help.
2d arrays in objective c
Nothing to do with me. I have never owned an iphone or tried to write code for one.
The whole point of NSMutableArray is that you don't care. Initialize both dimensions with an approximate size and then add to them. If your array grows beyond your initial estimate the backing storage will be increased to accomodate it. This counts for both your columns (first order array), and rows (second order array).
EDIT
Not sure what you meant in your comment. But this is one way to dynamically create a 2-dimensional mutable array in Objective-C.
NSUInteger columns = 25 ; // or some random, runtime number
NSUInteger rows = 50; // once again, some random, runtime number
NSMutableArray * outer = [NSMutableArray arrayWithCapacity: rows];
for(NSUInteger i; i < rows; i++) {
NSMutableArray * inner = [NSMutableArray arrayWithCapacity: columns];
[outer addObject: inner];
}
// Do something with outer array here
NSMutableArray can hold as many elements as you want to add to it. (Based on available heap though).
All you have to do is when you want to add an element(Array) to this mutable array you can add it using the addObject method.
So you create a MutableArray as follows:
NSMutabaleArray *outerArray = [NSMutableArray array]; // initially contains 0 elements.
[outerArray addobject:<anotherArray>];
From your rejection of the other answers I think you don't know how to add them in a loop, or am I wrong?
Try:
for (i = 0; i < mapSize; i++)
[outerArray addObject: [[NSMutableArray alloc] init]];
or, if you know or can estimate the size of the second dimension:
for (i = 0; i < mapSize; i++)
[outerArray addObject: [[NSMutableArray alloc]
initWithCapacity: your_2nd_d_size]];
Now, how you fill the arrays, i.e. where you get the contents depends on you. In one or more loops, you do:
[(NSMutableArray *)[outerArray objectAtIndex: i]
addObject: your_current_object];

NSIndexPath getIndexes method....how?

Am confused with the use of this method and the documentation that lists it as a (void) method.
"on return the index path's indexes"
where does it return anything too?
Should it not be:
- (NSIndexPath *)getIndexes:(NSUInteger *)indexes
getIndexes:
Provides a reference to the index path’s indexes.
- (void)getIndexes:(NSUInteger *)indexes
Parameters
indexes
Pointer to an unsigned integer array. On return, the index path’s indexes.
Availability
Available in iOS 2.0 and later.
Declared In
NSIndexPath.h
You have to allocate the NSUInteger array of size [indexPath length] and pass it as argument. The return value will be written there. You have to release that array yourself or do nothing it was created on stack like this:
NSUInteger array[[indexPath length]];
[indexPath getIndexes: array];
Maybe the next sentence explains the reason
It is the developer’s responsibility to allocate the memory for the C array.
It's actually a pointer to a C array that will be filled for you with the indexes, so there's no reason to additionally return it from the function - you already know its address.
You can use the function as follows
NSUInteger indexCount = [indices count];
NSUInteger buffer[indexCount];
[indices getIndexes:buffer maxCount:indexCount inIndexRange:nil];
You send that message to an instance of NSIndexPath, so getting one back wouldn't help. The -getIndexes: method fills the array 'indexes' with the indexes from the index path. So you'd do something like:
NSUInteger *indexes = calloc([indexPath length], sizeof(NSUInteger));
[indexPath getIndexes:indexes];
After that, indexes will be filled with the index values that are in indexPath.
Max, Thomas and Caleb, I have tried all three ways and cannot get anything to work, so maybe fired off the accepted solution too quick.....but probably more likely I just don't get it? I can't get the right size of the array in order to loop through it to access the required rows in my table. I would have though that calloc([indexPath length], sizeof(NSUInteger)) would for a group with 5 rows return an array with 5 rows with each row holding NSUIntegr.....or am of so far off beam it embarrassing?

Check whether data is present at an index in NSMutableArray

I have a NSMutableArray, I want to insert data inside it, the problem is first I want to check if the index where I'm inserting the data exists or not. How to do that?
I try something like that but nothing is working:
if ([[eventArray objectAtIndex:j] count] == 0)
or
if (![eventArray objectAtIndex:j])
if (j < [eventArray count])
{
//Insert
}
NSArray and NSMutableArray are not sparse arrays. Thus, there is no concept of "exists at index", only "does the array have N elements or more".
For NSMutableArray, the grand sum total of mutable operations are:
- (void)addObject:(id)anObject;
- (void)insertObject:(id)anObject atIndex:(NSUInteger)index;
- (void)removeLastObject;
- (void)removeObjectAtIndex:(NSUInteger)index;
- (void)replaceObjectAtIndex:(NSUInteger)index withObject:(id)anObject;
All other mutability methods can be expressed in terms of the above and -- more specifically to your question -- removing an object does not create a hole (nor can you create an array with N "holes" to be filled later).
I've given a brief implementation of a sparse array in this question: Sparse Array answer

Access NSMutableArray to an index with no value

if i try to access a nsmutableArray with objectAtIndex:x and if i have no object at this index, my app always crash.
So my question is: how can i check, if there is something at this index, without crashing the app?
I hope your understand my question.
Thanks, Alex
Check if your index is in array's bounds range:
if (index >=0 && index < [myArray count])
...
NSArray has a method called "count".
Call count on your mutable array and it will tell you the number of elements in the array.
You have two options: Use the count method to ensure you're within the bounds of the array, or catch the exception when you try to use objectAtIndex: Checking the range with count will be much lower overhead than catching the exception.
In case you didn't know - there are no "holes" allowed in an NSArray - the objects from index 0 to the end of the array ([array count]-1) will all be accessible.
If you are within the array's bounds, you will always have an object at a specific index between 0 and [array count], as the array cannot have gaps of nil values in it.
Try this
for(i=0; i< [myMutableArrayObject count]; i++) {
NSLog(#"%#",[[myMutableArrayObject objectAtIndex: i] myMethodDefined]);
}
Here, we are using a method predefined called count which returns the number of objects in the MutableArray Object
and then another method objectAtIndex which iterates the objects at the interval from 0 to (count – 1).
Regards,
Sumit