初始化 NSArray 并向其中添加 NSInteger
我在初始化 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将 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.
代码显示正确(从技术上讲,您应该使用
+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?