无法将对象添加到NS数组 [英] Cannot add object to NS array

查看:75
本文介绍了无法将对象添加到NS数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在头文件中定义了一组餐厅

I have an array of restaurants defined in the header file

@property (nonatomic, strong) NSMutableArray *resPool; //pool of restaurants

现在,我正在尝试通过在.m文件中执行以下操作,将餐厅对象添加到此数组中:

Now I am trying to add a restaurant object to this array by doing the following in the .m file:

id restaurant = [[Restaurant alloc] initWithResName:resName]
[self.resPool addObject:restaurant];// add the restaurant to the res pool array

当我运行此代码时,resName NSArray保持为空.我不确定自己在做什么.我只是想提起目标C.有人可以帮我吗?

The resName NSArray remains empty when I run this code. I am not sure what I'm doing wring. I am just trying to pick up objective C. Can someone help me with this?

推荐答案

您在任何地方都不会说resPool = [NSMutableArray alloc]init];.将内存分配给resPool,然后对其进行操作.通常,内存分配是在初始化程序中完成的.在您的任何类的init方法中执行此操作.

You are not saying resPool = [NSMutableArray alloc]init]; anywhere. Allocate memory to the resPool and then do operations on them. Generally, memory allocation is done in your initializer. Do this any of your class's init methods.

这篇关于无法将对象添加到NS数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆