简体   繁体   English

将NSMutablearray复制到另一个

[英]Copy NSMutablearray to another

I am trying to copy NSMutableArray to another but it does not show me anything at the UITableView : 我试图将NSMutableArray复制到另一个,但它没有在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? 它崩溃我的应用程序,因为我的NSMutableArray没有nil如何添加nil? addobject:nil does not work it crashes the app: addobject:nil不起作用它崩溃应用程序:

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;

Your initial call to alloc an NSMutableArray will most likely crash, since you don't have a nil terminator in your argument list. 您初始调用分配NSMutableArray很可能会崩溃,因为您的参数列表中没有nil终止符。

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

This might help you: 这可能对您有所帮助:

NSMutableArray *result  = [NSMutableArray arrayWithArray:array];

or 要么

NSMutableArray *result = [array mutableCopy]; //recommended

There are a few problems... One problem is that you're using 'initWithObjects' and adding the previous array. 有一些问题......一个问题是你正在使用'initWithObjects'并添加前一个数组。 This seems like unwanted behaviour since you most likely want to add the strings @"one" and @"two" to the array. 这似乎是不必要的行为,因为您很可能想要将字符串@“one”和@“two”添加到数组中。 You most likely intended to use initWithArray or addObjectsFromArray . 您很可能打算使用initWithArrayaddObjectsFromArray (Doing it the way you did, will add the NSMutableArray (not its objects) to the list) (按照你的方式做,将NSMutableArray(不是它的对象)添加到列表中)

The second problem, when you use initWithObjects, you need to list each of the objects and then terminate the list with a nil value. 第二个问题,当您使用initWithObjects时,您需要列出每个对象,然后使用nil值终止列表。 ( docs ) In other words, you need to use... docs )换句话说,你需要使用......

NSMutableArray *objectsToAdd = [[NSMutableArray alloc] initWithObjects:@"One", @"Two", nil];

问题可能是第4行的本地list声明与属性冲突。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM