将 NSInteger 对象添加到 NSMutableArray
我有一个 NSMutableArray
@interface DetailViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {
NSMutableArray *reponses;
}
@property (nonatomic, retain) NSMutableArray *reponses;
@end
,我试图在数组中添加 NSInteger 对象:
@synthesize reponses;
NSInteger val2 = [indexPath row];
[reponses addObject:[NSNumber numberWithInteger:val2]];
NSLog(@"the array is %@ and the value is %i",reponses, val2);
它不起作用,该对象未添加到数组中,这是控制台显示的内容:
the array is (null) and the value is 2
I have an NSMutableArray
@interface DetailViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {
NSMutableArray *reponses;
}
@property (nonatomic, retain) NSMutableArray *reponses;
@end
and i'm trying to add in my array NSInteger object:
@synthesize reponses;
NSInteger val2 = [indexPath row];
[reponses addObject:[NSNumber numberWithInteger:val2]];
NSLog(@"the array is %@ and the value is %i",reponses, val2);
it won't work the object was not added to the array, this is what console shows:
the array is (null) and the value is 2
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
@Omz:是的。确保已分配并初始化数组。请检查以下代码
我已经检查过并且它有效。
如果不再需要
数组
,请确保释放
它。@Omz: is right. Make sure you have the array allocated and initialized. Please check the following code
I have checked this and it works.
If
array
is no longer needed make sure yourelease
it.您没有初始化数组,因此当您尝试向数组添加内容时,它仍然是
nil
。You're not initializing your array, so it's still
nil
when you're trying to add to it.您尚未初始化响应数组。在您的代码中,可能是 viewDidLoad, do:
然后将对象添加到您的数组中。
那应该有效。
You have not initialized the responses array. In your code, may be viewDidLoad, do:
Then add the object to your array.
That should work.