Ham*_*aal 7 iphone cocoa-touch objective-c nsmutablearray nsinteger
我有一个NSMutableArray
@interface DetailViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {
NSMutableArray *reponses;
}
@property (nonatomic, retain) NSMutableArray *reponses;
@end
Run Code Online (Sandbox Code Playgroud)
我正在尝试添加我的数组NSInteger对象:
@synthesize reponses;
NSInteger val2 = [indexPath row];
[reponses addObject:[NSNumber numberWithInteger:val2]];
NSLog(@"the array is %@ and the value is %i",reponses, val2);
Run Code Online (Sandbox Code Playgroud)
如果没有将对象添加到数组中它将无法工作,这是控制台显示的内容:
the array is (null) and the value is 2
Run Code Online (Sandbox Code Playgroud)
@Omz:是的.确保已分配和初始化阵列.请检查以下代码
NSMutableArray *array = [[NSMutableArray alloc] init];
NSInteger num = 7;
NSNumber *number = [NSNumber numberWithInt:num];
[ar addObject:number];
NSLog(@"Array %@",array);
Run Code Online (Sandbox Code Playgroud)
我已经检查了这个并且它有效.如果array不再需要,请确保release它.
您没有初始化数组,因此nil当您尝试添加数组时它仍然存在。
self.responses = [NSMutableArray array];
//now you can add to it.
Run Code Online (Sandbox Code Playgroud)