Joe*_*jah 0 arrays iphone xcode dictionary objective-c
我有一个问题:我有一个NSMutableArray,我在多个视图中传递.在其中一个视图中,可以编辑该数组(NSMutableDictionary)中的对象的值.我这样做使用代码:
NSMutableDictionary *tester = [[NSMutableDictionary alloc] initWithDictionary:selectedItem copyItems:YES];
NSMutableString *newLoc = [[NSMutableString alloc] initWithString:locationField.text];
[tester setValue:newLoc forKey:@"Location"];
[selectedList replaceObjectAtIndex:[selectedList indexOfObject:selectedItem] withObject:tester];
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是在selectedList中替换该对象.它给出了错误*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI replaceObjectAtIndex:withObject:]: unrecognized selector sent to instance 0xa631e30'
它是有效的,如果我复制selectedList的副本数组中的新项目,并再次使用新值分配selectedList,但其他视图有问题在数组中再次找到相同的列表(新分配的位置).
TL; DR版本:如何编辑NSMutableArray中的值(通过替换?)?为什么不替换ObjectAtIndex工作?
编辑:确实是不可改变的.不过,主要问题仍然是:我有:
NSMutableDictionary *tester = [[NSMutableDictionary alloc] initWithDictionary:geselecteerdItem copyItems:YES];
[geselecteerdItem setValue:nieuweLoc forKey:@"Location"];
[geselecteerdeLijst replaceObjectAtIndex:[geselecteerdeLijst indexOfObject:tester] withObject:geselecteerdItem];
Run Code Online (Sandbox Code Playgroud)
当我使用:时[warningList replaceObjectAtIndex:[warningList indexOfObject:geselecteerdeLijst] withObject:keuzeLijst],它给了我一个outofbounds错误,因为geselecteerdeLijst数组的索引在warningList中明显改变了.有任何想法吗?
selectedList是一个不可变数组,不支持任何修改.不过你可以这样做:
NSMutableArray *tmp = [NSMutableArray arrayWithArray: selectedList];
[tmp replaceObjectAtIndex:[selectedList indexOfObject:selectedItem] withObject:tester];
Run Code Online (Sandbox Code Playgroud)
编辑:澄清,__NSArrayI是一个具体的不可变子类NSArray,__NSArrayM是一个可变的子类.你不应该依赖私有类名,但由于它们不言自明,你至少可以知道哪个是可变的,哪个不是.
| 归档时间: |
|
| 查看次数: |
979 次 |
| 最近记录: |