xcode 更新 NSArray 上的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13138546/
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
Updating values on NSArray
提问by Datenshi
I'v run into such problem. I need to update the values in my NSArray. And don't know a way to do it. Here's my array
我遇到了这样的问题。我需要更新 NSArray 中的值。而且不知道有什么办法。这是我的数组
NSArray *arrayWithInfo = [[NSArray alloc] initWithObjects:AMLocalizedString(@"Status", nil),AMLocalizedString(@"Call", nil),AMLocalizedString(@"Location", nil),AMLocalizedString(@"Control", nil),AMLocalizedString(@"Sim", nil),AMLocalizedString(@"Object", nil),AMLocalizedString(@"Info", nil),nil];
self.dataArray = arrayWithInfo;
[arrayWithInfo release];
To be more specific I have tableview initialized with this array. There is a possibility for user to use different localized strings, so I have to update it. By using [tableview reloadData];
i'v got the table to update, but the values in NSArray stay the same as they were initialized in first place.
更具体地说,我用这个数组初始化了 tableview。用户有可能使用不同的本地化字符串,所以我必须更新它。通过使用[tableview reloadData];
iv 来更新表,但 NSArray 中的值与它们最初初始化时的值保持一致。
So how to make array look up at the strings once again and get their new values?
那么如何让数组再次查找字符串并获取它们的新值呢?
回答by Shamsudheen TK
Use NSMutableArrayinstead of NSArray
使用NSMutableArray代替NSArray
NSMutableArray(and all other classes with Mutablein the name) can be modified.
NSMutableArray(以及名称中带有Mutable 的所有其他类)可以被修改。
回答by Mick MacCallum
You should be using an NSMutableArray. Doing so will allow you to change its values after instantiation.
您应该使用NSMutableArray。这样做将允许您在实例化后更改其值。
回答by Hymanslash
Your array doesn't need to be mutable here as the array seems to be all or nothing. You dont mention the requirement to delete some objects and not others. NSMutableArray
isn't needed. You want to write a lazy loading getter method for the array which reinstantiates it if the array doesnt exist.
你的数组在这里不需要是可变的,因为数组似乎是全有或全无。您没有提到删除某些对象而不是其他对象的要求。NSMutableArray
不需要。您想为数组编写一个延迟加载 getter 方法,如果该数组不存在,该方法会重新实例化它。
-(NSArray *)dataArray{
if (_dataArray){
return _dataArray;
}
_dataArray = NSArray *arrayWithInfo = [[NSArray alloc] initWithObjects:AMLocalizedString(@"Status", nil),AMLocalizedString(@"Call", nil),AMLocalizedString(@"Location", nil),AMLocalizedString(@"Control", nil),AMLocalizedString(@"Sim", nil),AMLocalizedString(@"Object", nil),AMLocalizedString(@"Info", nil),nil];
return _dataArray;
}
Then when you want to reload the tableView
然后当你想重新加载 tableView
self.dataArray = nil;
[tableView reloadData];
this destroys the old array, forcing it to be remade but with the new localisation.
这会破坏旧阵列,迫使它重新制作,但使用新的本地化。
EDIT:
编辑:
The issue is the array isn't storing the statement AMLocalizedString(@"Status", nil)
its storing the result of that statement, which is the localised string itself. There is no way to make the array re-evaluate that statement without either re-creating the whole array again or using an NSMutableArray
and changing all the objects. The lazy loading getter method is more in the objective-c style.
问题是数组没有存储语句,而是存储了该语句AMLocalizedString(@"Status", nil)
的结果,即本地化的字符串本身。如果不再次重新创建整个数组或使用 anNSMutableArray
并更改所有对象,则无法使数组重新评估该语句。延迟加载 getter 方法更多地采用 Objective-C 风格。
回答by Prine
You need to use the NSMutableArray
. The NSArray
is immutable.
您需要使用NSMutableArray
. 该NSArray
是不可改变的。