将 NSInteger 对象添加到 NSMutableArray

发布于 2024-12-22 16:37:12 字数 586 浏览 3 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

意犹 2024-12-29 16:37:12

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

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

我已经检查过并且它有效。
如果不再需要数组,请确保释放它。

@Omz: is right. 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.

﹎☆浅夏丿初晴 2024-12-29 16:37:12

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

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

You're not initializing your array, so it's still nil when you're trying to add to it.

self.responses = [NSMutableArray array];
//now you can add to it.
花开浅夏 2024-12-29 16:37:12

您尚未初始化响应数组。在您的代码中,可能是 viewDidLoad, do:

reponses = [[NSMutableArray alloc] init];

然后将对象添加到您的数组中。

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

那应该有效。

You have not initialized the responses array. In your code, may be viewDidLoad, do:

reponses = [[NSMutableArray alloc] init];

Then add the object to your array.

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

That should work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文