简体   繁体   English

将NSInteger对象添加到NSMutableArray

[英]adding NSInteger object to NSMutableArray

I have an NSMutableArray 我有一个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: 我正在尝试添加我的数组NSInteger对象:

@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

@Omz: is right. @Omz:是的。 Make sure you have the array allocated and initialized. 确保已分配和初始化阵列。 Please check the following code 请检查以下代码

NSMutableArray *array = [[NSMutableArray alloc] init];
NSInteger num = 7;
NSNumber *number = [NSNumber numberWithInt:num];
[ar addObject:number];
NSLog(@"Array %@",array);

I have checked this and it works. 我已经检查了这个并且它有效。 If array is no longer needed make sure you release it. 如果不再需要array确保将其release

You're not initializing your array, so it's still nil when you're trying to add to it. 你没有初始化你的数组,所以当你试图添加它时它仍然是nil

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

You have not initialized the responses array. 您尚未初始化响应数组。 In your code, may be viewDidLoad, do: 在您的代码中,可能是viewDidLoad,执行:

reponses = [[NSMutableArray alloc] init];

Then add the object to your array. 然后将对象添加到数组中。

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

That should work. 这应该工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM