ios 如何更新 NSMutableArray 中的对象?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7239617/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I update an object in NSMutableArray?
提问by Azhar
I am trying to update an object in NSMutableArray.
我正在尝试更新NSMutableArray 中的对象。
Product *message = (Product*)[notification object];
Product *prod = nil;
for(int i = 0; i < ProductList.count; i++)
{
prod = [ProductList objectAtIndex:i];
if([message.ProductNumber isEqualToString:prod.ProductNumber])
{
prod.Status = @"NotAvaiable";
prod.Quantity = 0;
[ProductList removeObjectAtIndex:i];
[ProductList insertObject:prod atIndex:i];
break;
}
}
Is there any better way to do this?
有没有更好的方法来做到这一点?
回答by Nekto
Remove lines:
删除行:
[ProductList removeObjectAtIndex:i];
[ProductList insertObject:prod atIndex:i];
and that will be ok!
没关系!
回答by KingofBliss
For updating, use
要更新,请使用
- (void)replaceObjectAtIndex:(NSUInteger)index withObject:(id)anObject
- (void)replaceObjectAtIndex:(NSUInteger)index withObject:(id)anObject
But it is not needed in this case, since you are modifying the same object.
但在这种情况下不需要它,因为您正在修改同一个对象。
回答by Pontus Granstr?m
You could start by using fast enumeration, which is faster and easier to read. Also, you don't need to remove and insert the object, you can just edit it in line. Like this:
您可以从使用fast enumeration开始,它更快更容易阅读。此外,您不需要删除和插入对象,您可以直接编辑它。像这样:
Product *message = (Product*)[notification object];
for(Product *prod in ProductList)
{
if([message.ProductNumber isEqualToString:prod.ProductNumber])
{
prod.Status = @"NotAvailable";
prod.Quantity = 0;
break;
}
}
(Is ProductList
an object? If it is, it should start with a lowercase letter: productList
. Capitalized names are for classes. Also, Status
and Quantity
are properties and should too start with a lowercase letter. I highly suggest you follow the Cocoa naming conventions.)
(是ProductList
一个对象吗?如果是,它应该以小写字母开头:productList
。大写的名称用于类。此外,Status
andQuantity
是属性,也应该以小写字母开头。我强烈建议您遵循Cocoa 命名约定。)
回答by Zverusha
Use -insertObject:atIndex:
or replaceObjectAtIndex:withObject:
.
使用-insertObject:atIndex:
或replaceObjectAtIndex:withObject:
。
回答by Praveen S
There are two approaches
有两种方法
- Create a new object and replace the old object with the new object
- 创建一个新对象并用新对象替换旧对象
for(int i = 0; i < ProductList.count; i++) { prod = [ProductList objectAtIndex:i]; if([message.ProductNumber isEqualToString:prod.ProductNumber]) { newObj = [[Product alloc] autorelease]; newObj.Status = @"NotAvaiable"; newObj.Quantity = 0; [ProductList replaceObjectAtIndex:i withObject:newObj]; break; } }
for(int i = 0; i < ProductList.count; i++) { prod = [ProductList objectAtIndex:i]; if([message.ProductNumber isEqualToString:prod.ProductNumber]) { newObj = [[Product alloc] autorelease]; newObj.Status = @"NotAvaiable"; newObj.Quantity = 0; [ProductList replaceObjectAtIndex:i withObject:newObj]; break; } }
Update the existing object:
更新现有对象:
for(int i = 0; i < ProductList.count; i++)
{
prod = [ProductList objectAtIndex:i];
if([message.ProductNumber isEqualToString:prod.ProductNumber])
{
prod.Status = @"NotAvaiable";
prod.Quantity = 0;
break;
}
}