Iphone 将NSInteger对象添加到NSMutableArray

Iphone 将NSInteger对象添加到NSMutableArray,iphone,objective-c,cocoa-touch,nsmutablearray,nsinteger,Iphone,Objective C,Cocoa Touch,Nsmutablearray,Nsinteger,我有一个NSMutableArray @interface DetailViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> { NSMutableArray *reponses; } @property (nonatomic, retain) NSMutableArray *reponses; @end 如果未将对象添加到阵列中,则无法正常工作,这是console显示的内容

我有一个NSMutableArray

@interface DetailViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {

NSMutableArray *reponses;
}
@property (nonatomic, retain) NSMutableArray *reponses;

@end
如果未将对象添加到阵列中,则无法正常工作,这是console显示的内容:

the array is (null) and the value is 2

您没有初始化数组,因此当您尝试添加到数组时,它仍然是
nil

self.responses = [NSMutableArray array];
//now you can add to it.

您尚未初始化响应数组。在您的代码中,可能是viewDidLoad,请执行以下操作:

reponses = [[NSMutableArray alloc] init];
然后将对象添加到数组中

NSInteger val2 = [indexPath row];
[self.reponses addObject:[NSNumber numberWithInteger:val2]];

那应该行。

@Omz:是的。确保已分配并初始化阵列。请检查以下代码

NSMutableArray *array = [[NSMutableArray alloc] init];
NSInteger num = 7;
NSNumber *number = [NSNumber numberWithInt:num];
[ar addObject:number];
NSLog(@"Array %@",array);
我已经检查了这个,它工作了。 如果不再需要
array
,请确保
释放它。

对于新手(me),要将NSNumber转换回NSInteger(从数组检索信息时),“NSInteger num=[number integerValue];”(谢谢7KV7)
NSMutableArray *array = [[NSMutableArray alloc] init];
NSInteger num = 7;
NSNumber *number = [NSNumber numberWithInt:num];
[ar addObject:number];
NSLog(@"Array %@",array);