初始化一个未知大小的 NSArray

发布于 2024-12-22 00:33:57 字数 499 浏览 3 评论 0原文

初始化数组后,为了设置所需位置的值,我使用的

[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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

萝莉病 2024-12-29 00:33:57

它是一个可变数组,您可以随时添加对象(到末尾)并插入对象(在中间):

NSMutableArray *nameArray = [[NSMutableArray alloc] init];
[nameArray addObject:@"test"];
[nameArray insertObject:@"another test" atIndex:0];
[nameArray removeObjectAtIndex:1];

您可以在任何有指向可变数组的指针的地方执行此操作。

It's a mutable array, you can add objects (to the end) and insert objects (in the middle) as well, whenever you like:

NSMutableArray *nameArray = [[NSMutableArray alloc] init];
[nameArray addObject:@"test"];
[nameArray insertObject:@"another test" atIndex:0];
[nameArray removeObjectAtIndex:1];

You can do this anywhere you have a pointer to the mutable array.

玻璃人 2024-12-29 00:33:57

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文