1

I have an array of restaurants defined in the header file

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

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

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?

0

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

I think you have not instantiated the array, thus you can't add an object to it.

Try to add this:

- (NSMutableArray *)resPool {
    if (!_resPool) _resPool = [[NSMutableArray alloc]init];
    return _resPool;
}

It's called lazy instansiation in Objective-C, and it's pretty common

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.