初始化 NSArray 并向其中添加 NSInteger

发布于 2024-09-08 22:36:01 字数 1072 浏览 9 评论 0原文

我在初始化 NSArray 并向其添加整数时遇到问题。这是我的代码,我在我想要完成的任务中对其进行了评论。添加对象时我的应用程序崩溃,但我不知道我是否正确清除了数组。

//CREATE AND INITIALIZE AN ARRAY
NSArray *ticket;                                                        
ticket = [[NSArray alloc] init];

//slotA,slotB,slotC are of type NSInteger that I am trying to add
//to the array (THIS CRASHES)

[ticket addObject:[NSNumber numberWithInt:slotA]];
[ticket addObject:[NSNumber numberWithInt:slotB]];
[ticket addObject:[NSNumber numberWithInt:slotC]];

//I never got to this line of code but I think it has to be wrong
//because this would throw the whole //array away. I dont want it
//to be thrown away I just wanna clear it out but keep it instanciated.

[ticket release];

我尝试了这个,但它说我“缺少一个哨兵函数调用”

NSArray *ticket;
NSString *sltA=[NSString stringWithFormat:@"%d", slotA];
NSString *sltB=[NSString stringWithFormat:@"%d", slotB];
NSString *sltC=[NSString stringWithFormat:@"%d", slotC];
ticket = [[NSArray alloc] initWithObjects:sltA,sltB,sltC];

另外,我是否必须将整数更改为字符串才能将它们放入数组中?

I am having problems initalizing an NSArray and adding integers to it. Here is my code, which I commented in what I am trying to accomplish. My app crashes when adding an object, but I don't know, if I am clearing the array correctly either.

//CREATE AND INITIALIZE AN ARRAY
NSArray *ticket;                                                        
ticket = [[NSArray alloc] init];

//slotA,slotB,slotC are of type NSInteger that I am trying to add
//to the array (THIS CRASHES)

[ticket addObject:[NSNumber numberWithInt:slotA]];
[ticket addObject:[NSNumber numberWithInt:slotB]];
[ticket addObject:[NSNumber numberWithInt:slotC]];

//I never got to this line of code but I think it has to be wrong
//because this would throw the whole //array away. I dont want it
//to be thrown away I just wanna clear it out but keep it instanciated.

[ticket release];

I tried this,but its saying I am "missing a sentinal function call"

NSArray *ticket;
NSString *sltA=[NSString stringWithFormat:@"%d", slotA];
NSString *sltB=[NSString stringWithFormat:@"%d", slotB];
NSString *sltC=[NSString stringWithFormat:@"%d", slotC];
ticket = [[NSArray alloc] initWithObjects:sltA,sltB,sltC];

Also, do I have to change the integers to string to put them in an array?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

只等公子 2024-09-15 22:36:01

将 NSArray 更改为 NSMutableArray。这将允许您添加和删除对象。

NSArray 是一个创建后就不能更改的数组。

NSMutableArray 只是 NSArray 的子类,并且具有许多函数可以帮助您在数组中的任意位置添加和删除对象。

Change NSArray into an NSMutableArray. This will let you add and remove objects.

NSArray is an array that is not suppose to be changed after it is created.

NSMutableArray is just a subclass of NSArray and has many functions that will help you add and remove objects at any point in the array.

二智少女 2024-09-15 22:36:01

代码显示正确(从技术上讲,您应该使用 +numberWithInteger: 而不是 +numberWithInt:,但它肯定不会导致崩溃。)您看到的崩溃是什么?你能发布 GDB/Xcode 的输出吗?

The code appears correct (technically you should use +numberWithInteger: instead of +numberWithInt:, but it's certainly not going to cause a crash.) What is the crash you see? Can you post the output from GDB/Xcode?

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