初始化一个未知大小的 NSArray
初始化数组后,为了设置所需位置的值,我使用的
[self.appName replaceObjectAtIndex:x withObject:[self.appCell objectAtIndex: 0]];
问题是,如果我初始化没有对象的 appName 数组,该数组将保持为空,我必须使用 initWithObjects 初始化它,然后才能工作。问题是我不知道数组的大小,如果我将其设置为:
NSMutableArray *nameArray = [[ NSMutableArray alloc] initWithObjects:@"test",@"test",@"test",@"test", nil];
self.appName = nameArray;
[nameArray release];
例如,从 0 到 3,但从位置 4 到后面的位置,在 ReplaceObjectAtIndex 之后,位置的值为 nil 值。怎么解决呢?谢谢
Once an Array is initialized, in order to set value of desired position, I am using
[self.appName replaceObjectAtIndex:x withObject:[self.appCell objectAtIndex: 0]];
Problem is that if I initialize appName array without objects, this array keeps empty and I must initialize it using initWithObjects and then works. Problem is that I do not know the size of an array and if I set it like:
NSMutableArray *nameArray = [[ NSMutableArray alloc] initWithObjects:@"test",@"test",@"test",@"test", nil];
self.appName = nameArray;
[nameArray release];
For example, works from 0 to 3 but from position 4 to following ones, after replaceObjectAtIndex, position has a nil value. How to solve it? Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它是一个可变数组,您可以随时添加对象(到末尾)并插入对象(在中间):
您可以在任何有指向可变数组的指针的地方执行此操作。
It's a mutable array, you can add objects (to the end) and insert objects (in the middle) as well, whenever you like:
You can do this anywhere you have a pointer to the mutable array.
查看 NSArray< 上的计数方法/a> 它给出数组中元素的数量。因此,请检查您的索引 x 是否小于计数,然后您可以删除该元素。
Look at the count methon on NSArray it gives you the number of elements in the array. So check that your index x is less than the count then you can remove the element.