簡體   English   中英

將NSMutablearray復制到另一個

[英]Copy NSMutablearray to another

我試圖將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];

什么都沒有出現! 怎么了?

它崩潰我的應用程序,因為我的NSMutableArray沒有nil如何添加nil? 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;

您初始調用分配NSMutableArray很可能會崩潰,因為您的參數列表中沒有nil終止符。

此外,您還有一個局部變量,列表和屬性列表。 確保你實例化你的想法。 您可能需要這樣做:

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

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

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

[self.list addObjectsFromArray:myArray];

這可能對您有所幫助:

NSMutableArray *result  = [NSMutableArray arrayWithArray:array];

要么

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

有一些問題......一個問題是你正在使用'initWithObjects'並添加前一個數組。 這似乎是不必要的行為,因為您很可能想要將字符串@“one”和@“two”添加到數組中。 您很可能打算使用initWithArrayaddObjectsFromArray (按照你的方式做,將NSMutableArray(不是它的對象)添加到列表中)

第二個問題,當您使用initWithObjects時,您需要列出每個對象,然后使用nil值終止列表。 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