Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy NSMutablearray to another

I am trying to copy NSMutableArray to another but it does not show me anything at the UITableView:

NSMutableArray *objectsToAdd= [[NSMutableArray alloc] initWithObjects:@"one",@"two"];

NSMutableArray *myArray = [[NSMutableArray alloc] initWithObjects:objectsToAdd,nil];

NSMutableArray *list = [[NSMutableArray alloc] init];

[self.list addObjectsFromArray:myArray];

Nothing show up! What is wrong?

It crashes my app because I do not have nil at my NSMutableArray how can I add nil to it? addobject:nil does not work it crashes the app:

static NSString * DisclosureButtonCellIdentifier = 
@"DisclosureButtonCellIdentifier";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: 
                         DisclosureButtonCellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
                                   reuseIdentifier: DisclosureButtonCellIdentifier]
            autorelease];
}
NSUInteger row = [indexPath row];

NSString *rowString =nil;

rowString = [list objectAtIndex:row];


cell.textLabel.text = rowString;

cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
[rowString release];
return cell;
like image 709
stefanosn Avatar asked Jan 13 '10 14:01

stefanosn


People also ask

What is the use of nsmutablearray?

The NSMutableArray class declares the programmatic interface to objects that manage a modifiable array of objects. This class adds insertion and deletion operations to the basic array-handling behavior inherited from NSArray. NSMutableArray is “toll-free bridged” with its Core Foundation counterpart, CFMutableArray.

How to copy a NumPy array to another array?

Many times there is a need to copy one array to another. Numpy provides the facility to copy array using different methods. There are 3 methods to copy a Numpy array to another array. This function returns a new array with the same shape and type as a given array. numpy.empty_like (a, dtype = None, order = ‘K’, subok = True)

When copying between multidimensional arrays the array behave like?

When copying between multidimensional arrays, the array behaves like a long one-dimensional array, where the rows (or columns) are conceptually laid end to end.

When copying from a reference-type array to a value type array?

When copying from a reference-type array to a value-type array, each element is unboxed and then copied. When copying from a value-type array to a reference-type array, each element is boxed and then copied.


2 Answers

Your initial call to alloc an NSMutableArray will most likely crash, since you don't have a nil terminator in your argument list.

Also, you have a local variable, list, and a property, list. Make sure you're instantiating what you think you are. You might need to do this:

NSMutableArray *objectsToAdd= [[NSMutableArray alloc] initWithObjects:@"one",@"two", nil];

NSMutableArray *myArray = [[NSMutableArray alloc] initWithObjects:objectsToAdd,nil];

self.list = [[NSMutableArray alloc] init];

[self.list addObjectsFromArray:myArray];
like image 158
Ben Gottlieb Avatar answered Sep 23 '22 00:09

Ben Gottlieb


This might help you:

NSMutableArray *result  = [NSMutableArray arrayWithArray:array];

or

NSMutableArray *result = [array mutableCopy]; //recommended
like image 26
Nghia Luong Avatar answered Sep 23 '22 00:09

Nghia Luong