[英]How to copy NSMutableArray value into another NSMutableArray in iPhone?
[英]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”添加到數組中。 您很可能打算使用initWithArray
或addObjectsFromArray
。 (按照你的方式做,將NSMutableArray(不是它的對象)添加到列表中)
第二個問題,當您使用initWithObjects時,您需要列出每個對象,然后使用nil值終止列表。 ( docs )換句話說,你需要使用......
NSMutableArray *objectsToAdd = [[NSMutableArray alloc] initWithObjects:@"One", @"Two", nil];
問題可能是第4行的本地list
聲明與屬性沖突。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.